views:

90

answers:

3

I have a big dictionary i constantly reference in my code so i have it initialized at the top:

import ...

myDictionary = {'a':'avalue','b':'bvalue',...}

code ...

But when i try to get values, some of the keys are not found. It appears as though Python is chopping my dictionary due to a size limit. I tried searching google but couldn't find anything on this.

I ended up dumping the key:value mappings into a separate file and wrote a function that would build the dictionary by reading in the file.

It would be nice to know why this is happening... even better to find a cleaner way to still have my dictionary.

EDIT: Dictionary has over 1,700 keys

+1  A: 

Python does not have a dictionary size limit. I've had dictionaries with well over 1 million keys. Could you post more of the code?

Mike Axiak
i wrote a simple test script where i pre-declared a big dictionary and just printed out the length: len(myDictionary) and it the reported size was not even close to what it was supposed to be. It seems that pre-declared dictionary have different max lengths (memory limits) than dictionaries built one key at a time...I have asked my friends to try and they see the same weird result... not gonan post a 1,700 key dictionary, its easy to make:
makoto
myDictionary = {'0':'0','1':'1','2':'2',...Try just 300
makoto
@makoto: What version of Python are you using? I just created a source file with a 1000000 element dict literal, and the length was correctly reported.
Laurence Gonsalves
@makato: Can you post full code on gist.github.com or something that exhibits this behavior? Also mention your platform and version of python. Thanks!
Mike Axiak
+1  A: 

One thing you might want to look for is that the keys in your dictionary are not duplicates. For example, in the following code:

>>> d = {'1': 'hello', '2': 'world', '1': 'new'}
>>> d
{'1': 'new', '2': 'world'}
>>> 

because I used the key '1' twice, only the last one appeared and thus I was left with a dictionary of size 2 rather than 3.

Muhammad Alkarouri
A: 

No problem with 300 here

>>> s=", ".join("'%s':'%s'"%(x,x) for x in range(300))
>>> d=eval("{%s}"%s)
>>> len(d)
300

or 10000

>>> s=", ".join("'%s':'%s'"%(x,x) for x in xrange(10000))
>>> d=eval("{%s}"%s)
>>> len(d)
10000
gnibbler