Is this a safe thing to do?
Terry Reedy
tjreedy at udel.edu
Thu Oct 9 12:32:15 EDT 2003
"Duncan Booth" <duncan at NOSPAMrcp.co.uk> wrote in message
news:Xns940F8C176F22Bduncanrcpcouk at 127.0.0.1...
> The answer is to iterate over a COPY of the list if you want to
modify it
> during the iteration:
or, when deleting items, carefully iterate backwards
x=[{'f':9},{'f':1},{1:3,'f':9},{'f':9},{'f':3}]
for i in range(len(x)-1,-1,-1):
if x[i]['f'] == 9:
del x[i]
>>> x
[{'f': 1}, {'f': 3}]
Terry J. Reedy
More information about the Python-list
mailing list