Why do these two loops not give the same result? (Yes, I know the second version is bad style. But I'd still expect it to give the same output.)
gen1 = range(3)
gen2 = range(3)
print("First attempt")
for i in gen1:
for j in gen2:
print(i,j)
gen1 = (i for i in range(3))
gen2 = (i for i in range(3))
print("Second attempt")
for i in gen1:
for j in gen2:
print(i,j)
Output using Python 3.6.9 (and I get the same results with Python 2.7.15):
First attempt
(0, 0)
(0, 1)
(0, 2)
(1, 0)
(1, 1)
(1, 2)
(2, 0)
(2, 1)
(2, 2)
Second attempt
(0, 0)
(0, 1)
(0, 2)