[docs] [issue16225] list.remove in for loop

Serhiy Storchaka report at bugs.python.org
Sun Oct 14 13:24:05 CEST 2012


Serhiy Storchaka added the comment:

It is safe to modify a sequence during iteration if it's size not increased.

>>> words = ['cat', 'window', 'defenestrate']
>>> for i, w in enumerate(words):
...     if len(w) > 6:
...         words[i] = w[:5] + '…'
... 
>>> words
['cat', 'window', 'defen…']

----------
nosy: +serhiy.storchaka

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue16225>
_______________________________________


More information about the docs mailing list