I was trying out some things with lists in the interactive interpreter and I noticed this:
>>> list = range(1, 11)
>>> for i in list:
... list.remove(i)
...
>>> list
[2, 4, 6, 8, 10]
Can anyone explain why it left even numbers? This is confusing me right now… Thanks a lot.
It isn’t safe to modify a list that you are iterating over.