Hash maps in Python: why dictionary lookups are O(1)
How a hash map finds one key among millions in one step: hash the key to a bucket, jump there, compare. Collisions included, in 14 lines of Python.
A hash map (a Python dictionary, a hash table) finds one key among millions in about one step. It doesn't search: it computes where the key must be, goes straight there, and checks the few keys it finds.
What does a post office have to do with it?
Picture a post office wall of numbered pigeonholes. A clerk doesn't look through every hole for your letter: the address says which hole it belongs in, so sorting a letter and finding it later are both one move.
A hash map is that wall. The pigeonholes are called buckets, and here there are eight of them, numbered 0 to 7. In Python each bucket is just an empty list.

How does a name become a slot number?
That is the job of the hash function. It turns a key into a number, and the number modulo the number of buckets picks the slot. Ours is deliberately simple: add up the character codes of the name, then take the remainder when dividing by 8.
For "bob" the codes are 98, 111 and 98. They sum to 307, and 307 % 8 = 3, so bob goes into slot 3. "put" stores the pair, the name and its value, in that slot.

| Name | Character codes | Sum | Sum % 8 |
|---|---|---|---|
| bob | 98 + 111 + 98 | 307 | 3 |
| amy | 97 + 109 + 121 | 327 | 7 |
| may | 109 + 97 + 121 | 327 | 7 |
What is a collision?
"amy" sums to 327 and lands in slot 7. "may" has the same letters in a different order, so it has the same sum and lands in slot 7 too. Two different keys in one bucket is a collision.

Collisions can't be avoided: there are far more possible names than slots. They aren't a problem either, because each slot holds a short list, and the new pair is simply appended after the one already there.
How does a lookup work?
"get" does the same three things every time: hash the name, jump to that slot, and compare the keys in it until one matches. For "may": the hash gives slot 7, the first key there is amy, which doesn't match, so skip it; the next is may, which does, so return 41.

What does the program look like?
This is the program from the video, line for line:
N = 8
rack = [[] for _ in range(N)] # 8 empty slots
def slot(name):
return sum(map(ord, name)) % N
def put(name, value):
rack[slot(name)].append((name, value))
put("bob", 12)
put("amy", 30)
put("may", 41)
def get(name):
for key, value in rack[slot(name)]:
if key == name:
return value
print(slot("may"), get("may")) # 7 41
Output:
7 41
We wrote our own hash on purpose. Python's built-in hash() of a string changes from one run to the next (it is randomized for security), so the slot numbers wouldn't be the same each time you ran the program.
Why is that O(1)?
Hashing a key and jumping to its bucket costs the same whether the map holds ten names or ten million. The only work that grows is comparing keys inside one bucket, and a good hash function spreads keys so evenly that buckets stay short. Real hash maps also add buckets as they fill up, keeping the average bucket at a size of about one.

| Operation | Search a list | Hash map (average) |
|---|---|---|
| Find a key | O(n) | O(1) |
| Insert a key | O(1) at the end | O(1) |
The "average" matters. If every key landed in the same bucket, a lookup would compare against all of them and become O(n). Our sum-of-codes hash is weak in exactly this way: every anagram of a name collides. Python's own dictionary uses a much better hash and stores keys with open addressing (probing for the next free slot) rather than lists, but the idea is the same: hash, jump, compare.
Try it
Change N = 8 to N = 16 and run the program again. Do amy and may still land in the same slot? Work out why before you run it.