How much space is reserved for an empty dictionary in python?

If we create an empty dictionary, for example: idict = {}

how many spaces are assigned to this dictionary? I know for a list, if we initialize a list like ilist = []

it will always oversize, first 4 spaces, then 8.
How about a dictionary?

+3


source to share


2 answers


Well, dictionaries don't store the actual string internally, it looks a bit like C / C ++ pointers, so you only get constant overhead information in the dictionary for each element.

Testing against

import sys
x = {}
sys.getsizeof(x)

      



The dictionary itself consists of several buckets, each of which contains:

  • the hash code of the object that is currently being stored (which is not predictable from the bucket position due to collision resolution the strategy used)

  • pointer to key object pointer to value

  • in general, at least 12 bytes for 32 and 24 bytes for the 64-bit version.

The dictionary starts with 8 empty buckets and resizes to double the number of entries when it reaches its capacity (currently (2n + 1) / 3).

+2


source


Honestly, it really works like associative map

in C++

.. If you've ever used C++

then .. If you see the Python Interpreter source code, you can see that it uses a section of heap memory to store data on two types and use pointers to specify one information to another in the same way as it map

works in C++

. On my system it is 280. Now that @Navneet said you can use sys.getsizeof to calculate the size. But remember that this is system dependent and therefore your system may not give you 280 bytes. Understand that if it is 280 bytes, it means that it uses a thin stream of multiple associative pointers to store the point in the data structure.



0


source







All Articles