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.
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.
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.
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.
If you are using the dictionary as you would use a hash table then the space complexity is O(n).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With