iterate over list while changing it
tjreedy at udel.edu
Thu Sep 24 23:03:58 CEST 2009
Torsten Mohr wrote:
> a = [1, 2, 3, 4, 5, 6]
> for i, x in enumerate(a):
If you change a list while iterating over, start at the tail.
> if x == 3:
del a[i] # you already have the item
> if x == 4:
no such list method, which mean you did not run the above before
> print "i", i, "x", x
> I'd like to iterate over a list and change that list while iterating.
> I'd still like to work on all items in that list, which is not happening
> in the example above.
> The conditions in the example are not real but much more complex
> in reality.
> Can anybody tell me how to do this?
> Thanks for any hints,
More information about the Python-list