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


Groups > comp.lang.python > #3860

Re: dictionary size changed during iteration

Path csiph.com!x330-a1.tempe.blueboxinc.net!feeder1.hal-mli.net!nx01.iad01.newshosting.com!newshosting.com!news-out.readnews.com!transit3.readnews.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
From Mark Niemczyk <prahamark@gmail.com>
Newsgroups comp.lang.python
Subject Re: dictionary size changed during iteration
Date Fri, 22 Apr 2011 05:39:23 -0700 (PDT)
Organization http://groups.google.com
Lines 33
Message-ID <44afa6ac-d151-4ebe-8b5d-16834193f4c7@glegroupsg2000goo.googlegroups.com> (permalink)
Reply-To comp.lang.python@googlegroups.com
NNTP-Posting-Host 207.229.149.157
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252
Content-Transfer-Encoding quoted-printable
X-Trace posting.google.com 1303475963 31388 127.0.0.1 (22 Apr 2011 12:39:23 GMT)
X-Complaints-To groups-abuse@google.com
NNTP-Posting-Date Fri, 22 Apr 2011 12:39:23 +0000 (UTC)
In-Reply-To <4db11eb5$0$10600$742ec2ed@news.sonic.net>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=207.229.149.157; posting-account=yjzYUgoAAAB_PKEzWUqDhDdSzxKMCeMa
User-Agent G2/1.0
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:3860

Show key headers only | 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