How much space is reserved for an empty dictionary in python?
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).
source to share
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.
source to share