python said : "1, 2, 3, 6, 7, manbo !"

Erik Max Francis max at alcyone.com
Wed Jan 21 22:30:59 EST 2004


Ben Finney wrote:

> Got a pointer to somewhere that says so?  Or at least describes the
> expected behaviour for iteration over mutable types?

Tutorial, section 4.2:

http://www.python.org/doc/current/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."

This is actually not specific to Python; in C++'s Standard Library, for
instance, there are complicated ramifications of modifying a container's
contents while you're actively maintaining and using an iterator over
it.

-- 
 __ Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
/  \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ Isn't the best defense always a good attack?
    -- Ovid



More information about the Python-list mailing list