RuntimeError: dictionary changed size during iteration

Robert Brewer fumanchu at amor.org
Wed Jan 19 16:38:06 EST 2005


Roman Suzi wrote:
> I think, the behaviour below is misfeature:
> 
> >>> [e for e in vars()]
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> RuntimeError: dictionary changed size during iteration
> >>> e = None
> >>> [e for e in vars()]
> ['e', '__builtins__', 'rlcompleter', '__file__', '_[1]', 
> 'atexit', '__name__',
> 'readline', '__doc__']

But not unexpected, since vars() returns a dictionary, and binding 'e'
changes that dictionary while you are iterating over it. Try either:

[e for e in vars().keys()]

or

e = None
[e for e in vars()]

or the generator expression if you have Python 2.4.


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org



More information about the Python-list mailing list