Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python's Itertools permutation wrong behavior?

Given a list l=[1,2] I am trying to create a product of permutations. In other words, I am trying to create the following.

from itertools import permutations
l=[1,2]
print([(e1, e2) for e1 in permutations(l) for e2 in permutations(l)])

The above code prints [((1, 2), (1, 2)), ((1, 2), (2, 1)), ((2, 1), (1, 2)), ((2, 1), (2, 1))] as expected.

However, if I use the code below,

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

from itertools import permutations
l=[1,2]
lp1 = permutations(l)
lp2 = permutations(l)
print([(e1, e2) for e1 in lp1 for e2 in lp2])

The code prints [((1, 2), (1, 2)), ((1, 2), (2, 1))] .

I guess this is due to lp1 and lp2 pointing to the same iterator. But I do not understand why this is the case. Is this a intended behavior or is this a bug?

>Solution :

Yes. permutation is a generator. You can use it only once.

Just to illustrate with an easier example, it is exactly as if you
tried

for i in range(3):
    for j in enumerate([1,2,3]):
         print(i,j)

To get

0 (0, 1)
0 (1, 2)
0 (2, 3)
1 (0, 1)
1 (1, 2)
1 (2, 3)
2 (0, 1)
2 (1, 2)
2 (2, 3)

And then were surprised that

range1=range(3)
range2=enumerate([1,2,3])
for i in range1:
    for j in range2:
         print(i,j)

was not working as expected, and gives:

0 (0, 1)
0 (1, 2)
0 (2, 3)

Because you need to recreate range2 for each i iteration. Otherwise, j with iterates only once. The 2 other times, the iterator is over.

(Edit note: initialy I used range(3) in my example for range2. But that is not a good example, since a range is a range, not a generator. So you can use it several times)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading