Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storage capacity of dictionary and want to store huge data into dictionary in python

I want to store huge data into a dictionary in python. Huge data may be around 21 GB. I wrote a snippet to do so. Storing the integer values inside the dictionary.

Code:

import timeit
import sys
dicts = {}
print "\n***Building dict..."
start = timeit.default_timer()
for j in range(0,5):
    for i in range(0,1000000):
        dicts[''+str(j)+str(i)] = i
        print str(i) + '-' + str(j)
        print "Size : ", sys.getsizeof(dicts)/1024/1024, " MB"
print "Total time of build dict", timeit.default_timer() - start 

During runtime, when I reached the size using **getsizeof(dicts)** around 1.2GB, it fails to store the values in dictionary but doesn't show any error. Does Dictionary has some capacity to store the data?

So, the question is how can I store huge data into the dictionary?

NOTE: Doesn't require to store the data in files or databases. because I want to retrieve the key, value pair very fast.

like image 520
iNikkz Avatar asked Nov 13 '14 14:11

iNikkz


People also ask

How do I store a large dictionary in Python?

If you just want to work with a larger dictionary than memory can hold, the shelve module is a good quick-and-dirty solution. It acts like an in-memory dict, but stores itself on disk rather than in memory. shelve is based on cPickle, so be sure to set your protocol to anything other than 0.

How much data can a dictionary in Python hold?

Save this answer. Show activity on this post. That is your environment's constraint and has nothing to do with Python dictionaries. So the answer to your question is: A python dictionary can hold as much as your environment allows it to.

How is data stored in a dictionary in Python?

Dictionaries are generally defined in Python by a list of comma-separated key-value pairs wrapped around curly braces ({}). Each key is separated from its associated value by a colon (:). You can also define a dictionary by using its constructor, dict() . It can accept a collection of key-value pairs.

What is the space complexity of dictionary Python?

If you are using the dictionary as you would use a hash table then the space complexity is O(n).


1 Answers

The limit of the size of a Python dict depends on the free memory the OS makes available. The problem is that as a dict grows (resizes) it has to copy itself to redistribute the keys so when the dictionary get really huge this process can start to require a lot more memory than is actually available.

like image 176
Jeffrey Swan Avatar answered Oct 11 '22 15:10

Jeffrey Swan