I did not expect this to work, since I was modifying the object being iterated over, but I didn't expect it to fail this way. I actually expected an exception to be raised.
>>> x = [1, 2, 3]
>>> for a in x:
... print a, x.pop(0)
...
1 1
3 2
>>> x
[3]
With a slightly larger range:
>>> x = [1, 2, 3, 4]
>>> for a in x:
... print a, x.pop(0)
...
1 1
3 2
>>> x
[3, 4]
And a little bit larger still:
>>> x = [1, 2, 3, 4, 5]
>>> for a in x:
... print a, x.pop(0)
...
1 1
3 2
5 3
>>> x
[4, 5]
It's like the for loop is creating a generator from the list, but comparing the "index" to the length of the list to decide when iteration is over.
It still seems like this should generate an exception though, not this bizarre behavior. Is there some reason it doesn't raise an exception?