Message172906
> But in general the advice should be: if you want to insert or remove elements during iteration, iterate over a copy.
I would expand this to cover changing the list in any way. I think the point being made is that iteration doesn't implicitly make a copy. There are cases where modifying the list in place can also yield unexpected results. For example (naive list reversal):
>>> words = ['cat', 'window', 'defenestrate']
>>> for i, word in enumerate(words):
... words[-i-1] = word
...
>>> words
['cat', 'window', 'cat'] |
|
Date |
User |
Action |
Args |
2012-10-14 19:38:42 | chris.jerdonek | set | recipients:
+ chris.jerdonek, georg.brandl, docs@python, serhiy.storchaka, Ian |
2012-10-14 19:38:42 | chris.jerdonek | set | messageid: <1350243522.79.0.0905439715454.issue16225@psf.upfronthosting.co.za> |
2012-10-14 19:38:42 | chris.jerdonek | link | issue16225 messages |
2012-10-14 19:38:42 | chris.jerdonek | create | |
|