Python's
dict, used everywhere from a ticker-to-price lookup to a memoisation cache in a dynamic program, promises lookup — but a dict is not magic, it is a hash table, and the claim is an expected-case guarantee that depends on a specific set of assumptions about how keys are mapped to slots. This note builds the hash table from a direct-address table up, covers the two standard collision-resolution strategies (chaining and open addressing), derives the expected-cost bound under simple uniform hashing, and introduces universal hashing as the fix for the fact that any fixed hash function has a pathological input that breaks it. Understanding this machinery is what separates "I use dictionaries" from "I know when a dictionary-keyed cache degrades to per lookup" — a distinction that matters the moment a keyed lookup sits in a hot loop.In this lesson
- Learning objectives
- From direct addressing to hashing
- Chaining
- Open addressing
- Universal hashing
Before you start
The full lesson, with worked examples and exercise solutions, is for members.
Read the full lessonNew to SelfMadeQuant.com? See membership options or explore the curriculum.