You are on page 1of 4

© 2019 MathByte Academy

Python Dictionaries are ubiquitous

Python dictionaries are everywhere you look!

namespaces

classes

modules

functions

sets

and of course, your own dicts

Dictionaries are such an important part of Python that a lot of time and effort was put into
making them as efficient as possible

key sharing compact dictionaries

© 2019 MathByte Academy


class Person:
Key Sharing PEP 412 def __init__(self, name, age):
self.name = name
john self.age = age
john = Person('John', 78) ['name', 'John']
['age', 78]
eric
eric = Person('Eric', 75) ['name', 'Eric']
['age', 75]
michael
michael = Person('Michael', 75)
['name', 'Michael']
['age', 75]

à multiple instances of the same class à instance attribute names are the same

john eric michael


'name' ['John', 'Eric', 'Michael']
à split-table dictionary
'age' [78, 75, 75]
© 2019 MathByte Academy
Compact Dictionaries hash('alex') à 3 (simplified – in
hash('john') à 1 reality we may
{'alex': Alex, 'john': John, 'eric': Eric}
hash('eric') à 6 have collisions!)

0 wasted space
1 ['john', John] [ ['—', '—', '—'],
2 1 [-6350376054362344353, 'john', John],
['—', '—', '—'], key order
3 ['alex', Alex]
3 [4939205761874899982, 'alex, Alex], different from
4 ['—', '—', '—'], insertion order
5 ['—', '—', '—'],
6 [6629767757277097963, 'eric', Eric]
6 ['eric', Eric]
]

values = [[4939205761874899982, 'alex, Alex], key order


[-6350376054362344353, 'john', John], same as
[6629767757277097963, 'eric', Eric]] insertion order

1 3 6
indices = [None, 1, None, 0, None, None, 2]
© 2019 MathByte Academy

You might also like