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,
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?
permutations()three times, once fore1and twice fore2. That's why the iterator in the second version gets exhausted.