Skip to content Skip to sidebar Skip to footer

How To Print List Skipping One Element Each Time In Python Without Numpy?

given samplelist = [100,101,102,103,104,105,106,107,108,109] then I want output as below: [100,[101,102,103,104,105,106,107,108,109]] [101,[100,102,103,104,105,106,107,108,109]]

Solution 1:

a simple list comprehension should do it

>>> samplelist = [100,101,102,103,104,105,106,107,108,109]
>>> [[el for el in samplelist if el isnot i] for i in samplelist]
[[101, 102, 103, 104, 105, 106, 107, 108, 109],
 [100, 102, 103, 104, 105, 106, 107, 108, 109],
 [100, 101, 103, 104, 105, 106, 107, 108, 109],
 [100, 101, 102, 104, 105, 106, 107, 108, 109],
 [100, 101, 102, 103, 105, 106, 107, 108, 109],
 [100, 101, 102, 103, 104, 106, 107, 108, 109],
 [100, 101, 102, 103, 104, 105, 107, 108, 109],
 [100, 101, 102, 103, 104, 105, 106, 108, 109],
 [100, 101, 102, 103, 104, 105, 106, 107, 109],
 [100, 101, 102, 103, 104, 105, 106, 107, 108]]

Basically it scans the list and for each element it produces the whole list excluding the current element.

Alternatively, you can use a generator expression

>>> g = ([el for el in samplelist if el isnot i] for i in samplelist)
>>> for x in g:
...     print(x)
... 
[101, 102, 103, 104, 105, 106, 107, 108, 109]
[100, 102, 103, 104, 105, 106, 107, 108, 109]
[100, 101, 103, 104, 105, 106, 107, 108, 109]
[100, 101, 102, 104, 105, 106, 107, 108, 109]
[100, 101, 102, 103, 105, 106, 107, 108, 109]
[100, 101, 102, 103, 104, 106, 107, 108, 109]
[100, 101, 102, 103, 104, 105, 107, 108, 109]
[100, 101, 102, 103, 104, 105, 106, 108, 109]
[100, 101, 102, 103, 104, 105, 106, 107, 109]
[100, 101, 102, 103, 104, 105, 106, 107, 108]

EDIT as per your new requirements (i.e. the skipped element must be included):

>>> g = ([i, [el for el in samplelist if el isnot i]] for i in samplelist)
>>> for x in g:
...     print(x)
... 
[100, [101, 102, 103, 104, 105, 106, 107, 108, 109]]
[101, [100, 102, 103, 104, 105, 106, 107, 108, 109]]
[102, [100, 101, 103, 104, 105, 106, 107, 108, 109]]
[103, [100, 101, 102, 104, 105, 106, 107, 108, 109]]
[104, [100, 101, 102, 103, 105, 106, 107, 108, 109]]
[105, [100, 101, 102, 103, 104, 106, 107, 108, 109]]
[106, [100, 101, 102, 103, 104, 105, 107, 108, 109]]
[107, [100, 101, 102, 103, 104, 105, 106, 108, 109]]
[108, [100, 101, 102, 103, 104, 105, 106, 107, 109]]
[109, [100, 101, 102, 103, 104, 105, 106, 107, 108]]

Solution 2:

use itertools.combinations():

import itertools
a = [100,101,102,103,104,105,106,107,108,109]
list(itertools.combinations(a, len(a)-1))[::-1]

Solution 3:

You can use a simple slicing within a list comprehension :

>>> [samplelist[:i]+samplelist[i+1:] for i,_ in enumerate(samplelist)]
[[101, 102, 103, 104, 105, 106, 107, 108, 109],
 [100, 102, 103, 104, 105, 106, 107, 108, 109],
 [100, 101, 103, 104, 105, 106, 107, 108, 109],
 [100, 101, 102, 104, 105, 106, 107, 108, 109],
 [100, 101, 102, 103, 105, 106, 107, 108, 109],
 [100, 101, 102, 103, 104, 106, 107, 108, 109],
 [100, 101, 102, 103, 104, 105, 107, 108, 109],
 [100, 101, 102, 103, 104, 105, 106, 108, 109],
 [100, 101, 102, 103, 104, 105, 106, 107, 109],
 [100, 101, 102, 103, 104, 105, 106, 107, 108]]

If you want to capture the omitted item :

>>> [[samplelist[i],samplelist[:i]+samplelist[i+1:]] for i,_ in enumerate(samplelist)]
[[100, [101, 102, 103, 104, 105, 106, 107, 108, 109]], [101, [100, 102, 103, 104, 105, 106, 107, 108, 109]], [102, [100, 101, 103, 104, 105, 106, 107, 108, 109]], [103, [100, 101, 102, 104, 105, 106, 107, 108, 109]], [104, [100, 101, 102, 103, 105, 106, 107, 108, 109]], [105, [100, 101, 102, 103, 104, 106, 107, 108, 109]], [106, [100, 101, 102, 103, 104, 105, 107, 108, 109]], [107, [100, 101, 102, 103, 104, 105, 106, 108, 109]], [108, [100, 101, 102, 103, 104, 105, 106, 107, 109]], [109, [100, 101, 102, 103, 104, 105, 106, 107, 108]]]

Post a Comment for "How To Print List Skipping One Element Each Time In Python Without Numpy?"