After some indepth reading, all documentation leads to state two things about dictionaries:
If that is the case, why does this dictionary only consume 368 bytes of RAM, when an empty dictionary takes 240 bytes, shouldn't this of resized 4x, e.g: 960 bytes?
>>> getsizeof(dict(a=1,b=2,c=3,d=4,e=5,f=6,g=7))
368
>>> getsizeof(dict(a=1,b=2,c=3))
240
Am I being misinformed or misunderstanding something core here? Did something change with regards to this information on python 3.7?
In other words, our dictionary, with nothing in it at all, consumes 240 bytes. Not bad; given how often dictionaries are used in Python, it's good to know that they don't normally consume that much memory.
That means you need about 1 MB of memory to store the entire dictionary. Of course you can deflate it (zip) and store in memory, with a compression rate of about 90%. That means 100 KB.
Memory Consumption by dict vs list of tuples. Dictionary occupies much more space than a list of tuples. Even an empty dict occupies much space as compared to a list of tuples. Example 1: As we can clearly see that there is a huge difference between memory consumption of both the datatypes when both are empty.
There is in principle no size limitation to a dictionary in Python, except the capacity of your available memory (RAM + Swap space).
import sys
dictt=dict()
array=[]
for i in range(0,1000):
dictt[i]=i
array.append(sys.getsizeof(dictt))
print(array)
numberarray=[]
for i in range(1,1001):
numberarray.append(i)
import matplotlib.pyplot as plt
plt.plot(numberarray,array)
plt.ylabel('memory')
plt.xlabel("items")
plt.show()
now visualize the question helps a lot. you got a nice debating topic.
you should refer: http://www.jessicayung.com/how-python-implements-dictionaries/
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