Skip to content Skip to sidebar Skip to footer

How Allocation Of Memory For `dict` In Python Works?

I was playing around with Dictionaries and found this. import sys Square1 = {} Square2 = {} Square3 = {} for i in range(1, 8): Square1[i] = i**2 for i in range(1, 11): S

Solution 1:

When you create an empty dictionary, it preallocates the memory in chunks for initial few references it can store. As the dictionary adds more key-value pairs, it needs more memory.

But it doesn’t grow with each addition; each time it needs more space, it adds some chunk of memory which can accommodate "X" amount of key-value pairs, and once "X" amount is filled, another chunk of memory is allocated to dictionary.

Here's a sample code to display change is dictionary's size as the count of keys increases:

import sys

my_dict = {}
print("Size with {} keys:\t {}".format(0, sys.getsizeof(my_dict)))

for i inrange(21):
    my_dict[i] = ''print("Size with {} keys:\t {}".format(i+1, sys.getsizeof(my_dict)))

Here's the output in Python 3.6.2:

#same size for key count 0 - 5 : 240 BytesSize with 0 keys:240Size with 1 keys:240Size with 2 keys:240Size with 3 keys:240Size with 4 keys:240Size with 5 keys:240#same size for key count 6 - 10 : 360 BytesSize with 6 keys:368Size with 7 keys:368Size with 8 keys:368Size with 9 keys:368Size with 10 keys:368#same size for key count 11 - 20 : 648 BytesSize with 11 keys:648Size with 12 keys:648Size with 13 keys:648Size with 14 keys:648Size with 15 keys:648Size with 16 keys:648Size with 17 keys:648Size with 18 keys:648Size with 19 keys:648Size with 20 keys:648

Also, dictionary just stores a reference of memory that holds the keys and values, and doesn't stores key-value itself as part of dict object. So neither the type nor the size of the data affect the result of sys.getsizeof() for the dictionary.

For example, size of both the below dicts is 280 Bytes

>>>sys.getsizeof({'a': 'a'})
280

>>>sys.getsizeof({'a'*100000: 'a'*1000000})
280

However here's the difference between size of 'a' V/s 'a' * 1000000:

>>>sys.getsizeof('a')
38

>>>sys.getsizeof('a'*1000000)
1000037

Solution 2:

Because a dictionary is a container and sys.getsizeof does not measure size of a container and all of its contents.

You can use this function or more info here.

Post a Comment for "How Allocation Of Memory For `dict` In Python Works?"