Dictionaries · Lecture 01 · 18 min
Creating Python Dictionaries
Build dictionaries with literals, the dict() constructor, comprehensions and fromkeys, and see the hash table that makes key lookup fast.
Keys, values and the one rule
A dictionary maps keys to values. Each key appears at most once, and asking for a key gives you back its value without searching through the whole collection. The simplest way to make one is a literal:
a = {'k1': 100, 'k2': 200}
aValues can be any object at all. Keys have one requirement: they must be hashable. Roughly, an object is hashable if hash(obj) gives an integer that never changes during the object’s lifetime, and objects that compare equal have equal hashes. Python’s immutable built-ins (numbers, strings, tuples of hashable things, frozenset, None) are hashable. Mutable containers (lists, dicts, sets) are not:
print(hash((1, 2, 3))) # a tuple of hashable elements: fine
for obj in ([1, 2, 3], ([1, 2], [3, 4]), {'a': 1}):
try:
hash(obj)
except TypeError as ex:
print('TypeError:', ex)Notice the middle case: a tuple is immutable, but this one contains lists, so its hash would depend on objects that can change. Python refuses to hash it. You’ll see exactly why that matters in a moment, when we look inside a dict.
So keys can be fairly rich objects, and values can be anything, including other dicts:
a = {('a', 100): ['a', 'b', 'c'], 'key2': {'a': 100, 'b': 200}}
aFunctions as keys
Functions are objects, and they’re hashable (their hash is based on their identity). That means you can use them as keys. Here’s a small “job table” that stores the arguments each function should be called with later:
def fn_add(a, b):
return a + b
def fn_inv(a):
return 1 / a
def fn_mult(a, b):
return a * b
funcs = {fn_add: (10, 20), fn_inv: (2,), fn_mult: (2, 8)}
for f in funcs: # iterating a dict gives its keys
print(f.__name__, '->', f(*funcs[f]))Iterating over .items() gives (key, value) pairs, which reads better than looking the value up again:
for f, args in funcs.items():
print(f.__name__, args, '->', f(*args))How a dictionary works inside
Why do keys need a hash, and why must it never change? Because a dict is a hash table. Here is the idea, in the compact layout CPython has used since 3.6.
A dict keeps two arrays:
- an entries array, where each new item is appended as a row
(hash, key, value), in insertion order; - a sparse index table whose size is a power of two (8 for a small dict). Each slot is either empty or holds the position of a row in the entries array.
To store d[key] = value, Python computes h = hash(key) and uses the low bits of h (in effect h % table_size) to pick a slot. If that slot is empty, the new row is appended to the entries and the slot records its position. If the slot is already taken by a different key, that’s a collision: Python moves on to another slot, chosen by a fixed probe sequence that also depends on h, until it finds a free one.
{'a': 1, 'b': 2, 'c': 3}. Each key's hash picks a slot in the index table; the slot points to the key's row in the dense entries array. Iteration simply walks the entries array, which is why order is preserved.To look up d[key], Python repeats the same steps: hash the key, go to the slot, and follow the probe sequence. At each occupied slot it checks the row: is the stored key the very same object (is)? If not, is the stored hash equal to h (a cheap integer comparison) and does the stored key == the one we’re looking for? The expensive == only runs when the hashes already match. If so, it returns the value. If it reaches an empty slot, the key isn’t there: KeyError.
That procedure explains the rules:
- Fast lookup. As long as hashes are well spread out, the first slot or two is the right one, whether the dict has 10 items or 10 million. Python also keeps the table at most about two-thirds full, growing it (and re-inserting every key) when needed, so free slots stay easy to find.
- Equal keys must have equal hashes. If
a == bbuthash(a) != hash(b), looking upbwould start in the wrong place and never finda. - A key’s hash must never change. A key is filed under the hash it had when it was inserted. If it later changed, lookups would go to the wrong slot. That’s why mutable containers like lists are not hashable: their “value” can change, and so would any hash based on it.
- Order comes for free. Iteration walks the dense entries array, so items come out in the order they were inserted.
You can watch the table grow: the size of a dict jumps in steps as it is resized, rather than growing by one item at a time.
import sys
d = {}
last = sys.getsizeof(d)
print(f'{0:>3} items: {last} bytes')
for i in range(1, 50):
d[i] = i
size = sys.getsizeof(d)
if size != last:
print(f'{i:>3} items: {size} bytes')
last = sizeThe compact layout arrived in CPython 3.6, which made dicts smaller and, as a side effect, ordered. In Python 3.7 that order became an official part of the language: every dict keeps its keys in insertion order. Older code sometimes used collections.OrderedDict just for this, or assumed no order at all. Some older notebook environments also displayed dicts with sorted keys, which hid the real order; print(d) always shows the true one.
The dict() constructor
dict() accepts several kinds of input.
Keyword arguments are tidy for string keys, but only work when every key is a valid identifier (so no 'first name', no 'class', no integers):
d = dict(a=100, b=200)
dAn iterable of pairs: each element must itself be an iterable of exactly two items, the key and the value. Tuples and lists can be mixed:
print(dict([('a', 100), ['b', 200]]))
print(dict(zip('abc', range(3)))) # zip produces pairs
try:
dict([('a', 1, 2)])
except ValueError as ex:
print('ValueError:', ex)Another mapping (optionally combined with keyword arguments, which win on conflicts):
d = {'a': 100, 'b': 200, 'c': {'d': 1, 'e': 2}}
new_dict = dict(d, b=999)
print(new_dict)
print(new_dict is d)
print(new_dict['c'] is d['c'])new_dict is a new dictionary, but look at the last line: the nested dict under 'c' is the same object in both. dict(d) makes a shallow copy: a new container whose entries reference the same values. You’ll see how to make independent copies later in this section.
Dictionary comprehensions
When you’re computing keys or values, a comprehension is the most readable option. It has the same structure as a list comprehension, with key: value in front:
keys = ['a', 'b', 'c', 'd']
values = (1, 2, 3, 4)
squares = {k: v ** 2 for k, v in zip(keys, values)}
evens = {k: v for k, v in zip(keys, values) if v % 2 == 0}
print(squares)
print(evens)Nested loops work too. This builds a grid of points and maps each (x, y) tuple to its distance from the origin. Tuples of numbers are hashable, so they make perfect keys:
import math
coords = (-2, -1, 0, 1, 2)
distances = {(x, y): math.hypot(x, y) for x in coords for y in coords}
print(len(distances))
print(distances[(1, 1)], distances[(-2, 0)])(For a plain zip of keys and values, dict(zip(keys, values)) is just as good and a little shorter.)
dict.fromkeys
fromkeys is a class method that builds a dict from an iterable of keys, giving every key the same value (None if you don’t supply one). Keys are inserted in the order the iterable produces them, and duplicates collapse into one:
counters = dict.fromkeys(['a', 'b', 'c'], 0)
print(counters)
print(dict.fromkeys('python'))
print(dict.fromkeys('banana'))fromkeys doesn’t copy the value: every key refers to the very same object. That’s harmless for 0 or None, but a mutable default is shared:
groups = dict.fromkeys(['x', 'y'], [])
groups['x'].append(1)
print(groups) # both keys see the change
print(groups['x'] is groups['y'])
groups = {k: [] for k in ['x', 'y']} # a fresh list per key
groups['x'].append(1)
print(groups)Key takeaways
- Keys must be hashable; values can be anything. Lists, dicts, sets, and tuples containing them can’t be keys.
- A dict is a hash table: the key’s hash picks a slot, collisions are resolved by probing, and
==confirms the match. That’s why equal keys need equal hashes and a key’s hash must never change. - CPython stores entries in a dense array in insertion order, and since Python 3.7 that order is guaranteed.
- Create dicts with literals,
dict(**kwargs),dict(pairs),dict(mapping), comprehensions ordict.fromkeys(keys, value). dict(other)is a shallow copy, andfromkeysshares one value object among all keys.