loading...
Cover image for Python Dictionary Inside Out

Python Dictionary Inside Out

readthroughmyglasses profile image Pranati ・2 min read

Structure of Internal Dictionary

The dictionary consists of a number of buckets. Each of these buckets contains

The hash code of the object that contains key-value pair.

A pointer to the key object.
A pointer to the value object.

This sums up to at least 12 bytes on a 32bit machine and 24 bytes on a 64bit machine.

The dictionary starts with 8 empty buckets. This is then resized by doubling the number of entries whenever its capacity is reached.

  1. An empty python dictionary consumes 240 bytes

    d = {}
    import sys
    sys.getsizeof(d)
    240

  2. The first time we create dictionary it contains only 8 slots that can be filled with key-value pairs.

    d['python'] = 1
    sys.getsizeof(d)
    240

    As you can see the size of dictionary is still same after adding something to it. The dictionary

  3. The key value pair are not stored in dictionary itself, the hash value of key is stored

  4. Python dictionaries are implemented as hash tables behind the scene. Each key is hashed.

    The hash value tells at which memory location that key-value pair is stored, this in return saves lookup time.

  5. The key-values not stored in the dictionary itself the size of dictionary doesn't change even if we increase the size of its value

    d['a'] = 'a' * 100000
    sys.getsizeof(d)
    240
    sys.getsize('a')
    50

  6. if we remove the items from an dictionary the size of the dictionary will still be same.

    for key in list(d.keys()):
    d.pop(key)
    len(d)
    0
    sys.getsizeof(d)
    1184
    here, you can see the dictionary hasn't released the memory it has allocated

  7. If we empty the dictionary the size of it is 72 bytes less than the initialized empty dictionary i.e 240 bytes

    d.clear()
    len(d)
    0
    sys.getsizeof(d)
    72



Discussion

pic
Editor guide