While answering a particular question here in SO I stumbled upon a peculiar issue which I couldn’t explain. Unfortunately the first two Google Search page returned one SO Page which was also not helpful.
The Problem Code
>>> somedata=[random.randint(1,1000) for i in xrange(1,10000)]
>>> somehash=collections.defaultdict(int)
>>> for d in somedata:
somehash[d]+=1
>>> maxkey=0
>>> for k,v in somehash.iteritems():
if somehash[maxkey] > v:
maxkey=k
Traceback (most recent call last):
File "<pyshell#700>", line 1, in <module>
for k,v in somehash.iteritems():
RuntimeError: dictionary changed size during iteration
>>> for k,v in somehash.iteritems():
if somehash[maxkey] > v:
maxkey=k
>>>
And due to some odd reason, the first time I am iterating over the dictionary, Python is creating tantrums but the subsequent executions are fine as you can see in the example, the first time I iterated over the dictionary, it gave the Run Time Error but the next Time it didn’t complain.
Any Idea what might be going wrong?
Just in case if required
>>> sys.version_info
sys.version_info(major=2, minor=7, micro=0, releaselevel='final', serial=0)
>>> sys.version
'2.7 (r27:82525, Jul 4 2010, 09:01:59) [MSC v.1500 32 bit (Intel)]'
OS: Microsoft Windows [Version 6.1.7601] (Windows 7)
Adding or deleting items of a dictionary while iterating over it is an error. Since
somehashis adefaultdict, even what seems like a read-only access in the linemight add a new key — resulting in the error you encountered.