Chris Jerdonek added the comment: > 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'] ---------- _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue16225> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com