Conventions for iterators

Jason Orendorff jason at
Fri Feb 1 06:47:02 CET 2002

> So, obviously, behavior 2 is "best."  But I was wondering what the
> python community expects/requires.  Is the idiom such that solution 2 is
> "required," and everyone will expect an object's iterator to work that
> way, or is it more of a caveat emptor situation, and you had better red
> the module's documentation to see how the module handles this situation?

Caveat emptor.  Look at what dictionaries do.

x = {'a': 3, 'b': 9, 'c': 12, 'd': 18}
for k in x:
    if x[k] > 9:
        del x[k]

RuntimeError: dictionary changed size during iteration

## Jason Orendorff

More information about the Python-list mailing list