Reading preferences
Concept

Hash Tables

Use keys to find values, and see why different keys can share a bucket.

hash table, hash, key, value, bucket, collision, dictionary

Find a price by name

Imagine a small fruit stand. Instead of remembering a position in a row, we ask for the price of an apple. Here apple is a key, and its price is the value associated with that key.

A hash table stores key-value entries and uses a calculation on a key to help find where to look. A bucket is a place the table uses to organise entries. A hash function turns a key into a hash; the table uses it to choose where to search.

Why use it and where it helps

Looking up an item by a name, code, or other key is common: a fruit stand could keep prices by fruit name, or a program could count words by using each word as a key. We do not have to search a whole unsorted list one item at a time when the hash table's lookup works well.

The table does not know what a key means. It needs a valid hash and an equality check to tell whether a stored key is the one requested. A hash is not an encryption method, a unique identifier, or a promise of one comparison.

Follow a collision

For this tiny teaching example, suppose there are five buckets numbered 0 to 4 and we choose a bucket using key % 5. The % operation gives a remainder.

KeyRemainder after dividing by 5Bucket
1000
1500
1222

Two different keys, 10 and 15, both land in bucket 0. This is a collision. One way to handle it is separate chaining, where a bucket keeps a small chain of entries:

bucket 0: [10 -> tea] -> [15 -> rice]
bucket 1: empty
bucket 2: [12 -> beans]

To find key 15 here, go to bucket 0 and compare keys until 15 matches. A table can also use other collision strategies. This illustration uses a made-up hashing rule; it does not describe Python's actual dictionary layout.

See the reasoning

Follow the example one step at a time

Follow a collision using five buckets and a toy remainder rule. This is a prepared teaching demonstration, not live code execution.

Start

bucket 0: empty   bucket 2: empty

This example selects a bucket using key % 5, not Python's actual dictionary layout.

Python illustration

Python's dict is a built-in mapping backed by a hash table in CPython. These operations use fruit names as keys:

prices = {"apple": 3, "pear": 5}
print("apple:", prices["apple"])
prices["apple"] = 4
prices["plum"] = 6
print("updated:", prices["apple"])
print("plum present:", "plum" in prices)
print("peach present:", "peach" in prices)
print("peach value:", prices.get("peach"))

Prepared output — not live execution:

apple: 3
updated: 4
plum present: True
peach present: False
peach value: None

Assigning an existing key replaces its value; assigning a new key adds an entry. prices["peach"] would raise KeyError if absent. get returns None by default for a missing key, but None could also be a stored value; use in when the distinction matters.

Time, space, and common mistakes

Under suitable hashing and load assumptions, lookup and insertion take expected O(1) time for fixed-cost hashing and equality. In the worst case, many collisions can require O(n) work. An insertion can also occasionally trigger a resize. Storing n entries takes O(n) space.

Keys in a Python dictionary must be hashable: their hash must remain stable while used as keys, and equal keys must have equal hashes. Lists are not hashable; strings and integers are examples of hashable keys. Python dictionaries preserve insertion order, but that is not sorted order and should not be confused with a bucket arrangement. Hashing or comparing long keys can itself take time, so one lookup is not automatically constant-cost.

Reflect on the concept

Four fixed questions to reinforce the reasoning. Answers are not saved or scored.

1. In a key-value table of fruit prices, what does the key identify?
2. What is a hash collision in our bucket example?
3. How can we check whether peach is a key in the Python dictionary prices?
4. Does a hash table guarantee O(1) lookup in every situation?

References and further reading