First of all a disclaimer:
I don't want to use a code like this, I am aware it is a bad practice. As well I am not interested in tips on how to improve it, to make it right. What interests me is a theory.
How come code like this works in python 3.6:
ls = range(5)
for elem_a in ls:
ls = range(5, 10)
for elem_b in ls:
print(elem_a, elem_b)
I am reassigning the value of ls while iterating through it. Is the value of ls in the first iteration stored in memory during the first execution of for elem_a in ls?