Skip to content

Hash Table Basics

Lesson 1 of 8 · 4 min

Hash Table Basics

Step 1 of 6

A hash table is numbered bins. The key itself decides which bin — no shelf-walking, no sorting.

The Idea

A hash table runs each key through a hash function that returns a bucket number. Storing and looking up both jump straight to that bucket, so the average cost stays O(1) however many keys are inside. The price: keys must be hashable, and order is never promised.

Real-World Example

A pharmacy files finished prescriptions into numbered bins using the last two digits of the order number. Nobody walks the shelves reading labels — the number on your slip is the bin. Two hundred waiting bags cost the same lookup as five.

The Code

Your turn

What does this print?

shelf = {"a": 1, "b": 2}
shelf["c"] = 3
shelf["a"] = 9
print(len(shelf), shelf["a"])

Mini quiz

1 / 3

What does a hash function produce from a key?