http://docs.python.jp/3/tutorial/controlflow.html#for-statements
If you need to modify an iterating sequence inside a loop (for example, to duplicate a selected item), it's a good idea to make a copy first. Iterations to sequences do not implicitly make a copy. Slice notation makes this especially useful:
>>> for w in words[:]: # Loop over a slice copy of the entire list.
... if len(w) > 6:
... words.insert(0, w)
...
>>> words
['defenestrate', 'cat', 'window', 'defenestrate']
In the above example, the story gets confusing if the list itself changes while turning in an iterator. I understood that it would be better to use slice notation in such cases (maybe wrong).
Recommended Posts