0

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

johnsyweb
  • 136,902
  • 23
  • 188
  • 247
makoto
  • 9
  • 1
  • 2
  • how many keys does the dict have? – aaronasterling Sep 15 '10 at 23:11
  • I'd store that dictionary in a file. It's no problem for a dictionary, but kind of excessive for a line of code. – recursive Sep 15 '10 at 23:27
  • 1700 is nothing. You have mis-loaded the dictionary. Your bug is in your loading, not in the dictionary itself. Post the smallest piece of loading code that demonstrates the error. – S.Lott Sep 15 '10 at 23:51
  • Looks good here (python 2.6.6): > d = {} > for i in xrange(1700): > d[unicode(i)] = i > len(d) > 1700 – monkut Sep 16 '10 at 01:44
  • `dict( (x,x) for x in range(10000) )` A 10,000 element dictionary. Works great. – S.Lott Sep 16 '10 at 18:22

2 Answers2

4

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
  • 23,884
  • 19
  • 66
  • 101
2

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
  • 11,827
  • 2
  • 33
  • 49
  • 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 Sep 15 '10 at 23:35
  • myDictionary = { '0':'0', '1':'1', '2':'2', ... Try just 300 – makoto Sep 15 '10 at 23:36
  • 1
    @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 Sep 15 '10 at 23:43
  • 2
    @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 Sep 15 '10 at 23:45