Unexpected Behavior Iterating over a Mutating Object

Delaney, Timothy (Tim) tdelaney at avaya.com
Tue Sep 13 17:45:55 EDT 2005


Dave Hansen wrote:

> Again, iterating over an item that is mutating seems like a Bad
> Idea(tm) to me.

Absolutely. It can be safe to do it, but only if the iterator in
question supports it, and all modifications occur through the iterator
(this is how Java does it). In a Python for loop, the actual iterator is
never exposed in any case, so this is not possible - it's just like
using the new Java foreach syntax: for (element : iterable), but more
flexible.

> But I was curious: is this the intended behavior, or
> does this fall under what C programmers would call 'undefined
> behavior.'

It's intended. From the tutorial (section 4.2 - for statement):
http://docs.python.org/tut/node6.html#SECTION006200000000000000000

    It is not safe to modify the sequence being iterated over in the
    loop (this can only happen for mutable sequence types, such as
    lists). If you need to modify the list you are iterating over
    (for example, to duplicate selected items) you must iterate over
    a copy. The slice notation makes this particularly convenient: 

        >>> for x in a[:]: # make a slice copy of the entire list
        ...    if len(x) > 6: a.insert(0, x)
        ... 
        >>> a
        ['defenestrate', 'cat', 'window', 'defenestrate']


Tim Delaney



More information about the Python-list mailing list