Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #3860

Re: dictionary size changed during iteration

From Mark Niemczyk <prahamark@gmail.com>
Newsgroups comp.lang.python
Subject Re: dictionary size changed during iteration
Date 2011-04-22 05:39 -0700
Organization http://groups.google.com
Message-ID <44afa6ac-d151-4ebe-8b5d-16834193f4c7@glegroupsg2000goo.googlegroups.com> (permalink)

Show all headers | View raw


As of Python 3.x (which I suspect you are running):

"The objects returned by dict.keys(), dict.values() and dict.items() are view objects. They provide a dynamic view on the dictionary’s entries, which means that when the dictionary changes, the view reflects these changes.", and 

"Iterating views while adding or deleting entries in the dictionary may raise a RuntimeError or fail to iterate over all entries."

     see: http://docs.python.org/release/3.1.3/library/stdtypes.html#dict-views

On my system:
ActivePython 3.2.0.0 (ActiveState Software Inc.) based on
Python 3.2 (r32:88445, Feb 21 2011, 11:25:33) 
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "copyright", "credits" or "license()" for more information.
>>> ukeys = {}
>>> type(ukeys.keys())
<class 'dict_keys'>
>>> 

So, to achieve your objective, one solution could be to change the ukeys assignment statement to:

     ukeys = list(self.updates.keys())



Hope this helps,

Mark N.

Back to comp.lang.python | Previous | Next | Find similar


Thread

Re: dictionary size changed during iteration Mark Niemczyk <prahamark@gmail.com> - 2011-04-22 05:39 -0700

csiph-web