How do I iterate through combinations of a list [duplicate] - python

This question already has answers here:
Making all possible combinations of a list [duplicate]
(6 answers)
Closed 6 years ago.
I have a list of elements, say
list = [1, 2, 3, 4]
and I would like to iterate through couples of distinct elements of this list, so
for x, y in some_iterator(list):
print x, y
should show
1 2
1 3
1 4
2 3
2 4
3 4
Note that I don't want all combinations of list as in this question. Just the combinations of a given length.
What would be the most pythonic way of doing this ?
What if I wanted to do the same with n-uples ? For instance with combinations of 3 elements out of n
for x, y, z in another_iterator(list):
print x, y, z
would show
1 2 3
1 2 4
2 3 4

Use itertools.combinations:
from itertools import combinations
for combo in combinations(lst, 2): # 2 for pairs, 3 for triplets, etc
print(combo)

Related

Python - Iterate over 2 lists of a different length in a specific way [duplicate]

This question already has answers here:
How to zip two differently sized lists, repeating the shorter list?
(15 answers)
Closed 7 months ago.
I have 2 lists of a different length
list_1 = [1, 2, 3, 4, 5]
list_2 = ['a', 'b']
If I'd do:
for (i,j) in itertools.zip_longest(list_1, list_2):
print (i,j)
The output would be
1 a
2 b
3 None
4 None
5 None
What I want is to iterate over those 2 lists, but repeat the shorter list when it gets exhausted any amount of times while the longer list still iterates. In this example I'd want output to be
1 a
2 b
3 a
4 b
5 a
There's an itertools for that. You want to cycle the second list and do a vanilla zip on the first. cycle will remember and reemit values from list_2 and zip will stop at the end of list_1.
>>> import itertools
>>> list_1 = [1, 2, 3, 4, 5]
>>> list_2 = ['a', 'b']
>>> for i,j in zip(list_1, itertools.cycle(list_2)):
... print(i, j)
...
1 a
2 b
3 a
4 b
5 a
if you want the result to always be the longer of the two lists (either could cycle), you'd need to choose which one uses itertools.cycle. There are a dozen ways to do that, but here's one
>>> zipper = zip(list_1, itertools.cycle(list_2)) if len(list_1) >= len(list_2) else zip(itertools.cycle(list_1), list_2)
>>> for i, j in zipper:
... print(i, j)
...
1 a
2 b
3 a
4 b
5 a
And if you want something that works for iterators in general (they wouldn't know their size in advance), you could make them into a list first.

In Python how to unpack the sublists in the nested list while printing

consider a list l=[1,2,3,4,5].
if we want to unpack the list and also to print, we use * operator to unpack
l=[1,2,3,4,5]
print(*l,sep="\n")
output:
1
2
3
4
5
It is in case of single simple list.
If I have nested list and want to unpack all the sublists like aboveā˜.
consider a sublist sl=[[1,2,3],[4,5,6],[7,8,9]]
If I put ** in the print satement it throws an error message.
sl=[[1,2,3],[4,5,6],[7,8,9]]
print(**sl,sep="\n")
It doesn't work.
I want the output as
1
2
3
4
5
6
7
8
9
Is there any chance to unpack the sublists of nested list without loops
You can use itertools.chain like below:
>>> from itertools import chain
>>> sl=[[1,2,3],[4,5,6],[7,8,9]]
>>> print(*(chain.from_iterable(sl)),sep="\n")
1
2
3
4
5
6
7
8
9
You could flatten the list and then unpack it.
l = [[1,2,3],[4,5,6],[7,8,9]]
print(*[elt for sl in l for elt in sl])
To get the output you require you could do this:
sl=[[1,2,3],[4,5,6],[7,8,9]]
print('\n'.join([str(x) for y in sl for x in y]))

Change initial element of a list keeping the sequence [duplicate]

This question already has answers here:
Efficient way to rotate a list in python
(27 answers)
Circular list iterator in Python
(9 answers)
Closed 2 years ago.
I have an algorithm that outputs a list in a specific order, for example:
[0 4 3 2 1 5]
I want to reorder the list to start with the element '1' and keep the sequence, so my output would be:
[1 5 0 4 3 2]
I've searched and tried different possibilities but I'm still struggling with it.
How can I make this work?
lst = [0, 4, 3, 2, 1, 5]
to rotate it into position:
i = lst.index(1)
lst = lst[i:] + lst[:i]

mergering two for loops to map 1 value to other 1 to1 mapping [duplicate]

This question already has answers here:
How to merge lists into a list of tuples?
(10 answers)
Closed 3 years ago.
a =['1','2','3']
b =['A','B','C']
for i in a:
print(i)
output:
1
2
3
for i in b:
print(i)
output:
A
B
C
I want to map 1 , A
2, B
3, C
so that i can store the value in DB in one for loop parallely
for i , n in a,b:
print i,n
This is not allowed
Use the zip() builtin.
a = ['1','2','3']
b = ['A','B','C']
for i, n in zip(a, b):
print(i, n)

how to pick items from different list inside for loop? [duplicate]

This question already has answers here:
An elegant and fast way to consecutively iterate over two or more containers in Python?
(10 answers)
Closed 4 years ago.
i have two list and i want to use items from both lists inside for loop
here's my code
list1 = ['a','b','c']
list2 = ['1','2','3']
for x in list1:
print(x)
for y in list2:
print(y)
This is what it prints
a
1
2
3
b
1
2
3
c
1
2
3
this is want i want it to print
a
1
b
2
c
3
list1 = ['a','b','c']
list2 = ['1','2','3']
for x,y in zip(list1,list2):
print(x)
print(y)
zip iterates the lists together.

Categories