google at mrabarnett.plus.com
Sun Mar 22 14:54:05 CET 2009
> Can you explain me this behaviour:
>>>> s = [1,2,3,4,5]
>>>> g = (x for x in s)
> [1, 2, 3, 4, 5]
>>>> del s
> [2, 3, 4, 5]
> Why next(g) doesn't give me 2?
First it yields s (which is 1), then you delete s, then it yields
s (which is now 3). It doesn't yield 2 because that's in now s,
and it's already yielded s.
In general you shouldn't modify what you're iterating over because the
behaviour depends on how it happens to be implemented.
More information about the Python-list