hash table
A hash table, also called a hash map or hashmap, is a data structure that maps keys to values and supports fast lookup, insertion, and deletion by computing an array index directly from each key. A hash function turns each key into a hash value, an integer that maps to a slot in an underlying array. From there, the value either settles into a free slot or runs into a collision with another key.
Here’s a diagram of a hash table in action:
On average these operations take constant O(1) time, which makes hash tables the default choice for lookup-heavy work. The catch is collisions, where two distinct keys map to the same slot.
Implementations resolve collisions by chaining, which keeps colliding entries in a per-slot list, or by open addressing, which stores every entry inside the table itself and follows a probe sequence until it finds an empty slot.
Try it on the chained hash table below: insert keys to watch them hash into slots and collide, then find one to see the lookup hash straight to its slot and scan only that short chain. To keep the arithmetic visible, the demo uses a simple stand-in hash that sums the key’s character codes:
When too many collisions pile up, lookup performance can degrade toward O(n). A hash table avoids this by tracking its load factor, the ratio of stored entries to the number of slots. Once that ratio exceeds a set threshold, the table resizes and rehashes its contents into a larger array.
Hash tables require keys to be hashable and usually immutable, so a key’s hash never changes while it is stored.
The structure is the foundation of Python’s built-in dictionary and set. Other languages ship it under the hash map name, such as Java’s HashMap, while C++ calls it unordered_map. Some languages reserve map for the key-value abstraction and hash table for the structure beneath it, but in most settings the names are interchangeable.
Related Resources
Tutorial
Build a Hash Table in Python With TDD
In this step-by-step tutorial, you'll implement the classic hash table data structure using Python. Along the way, you'll learn how to cope with various challenges such as hash code collisions while practicing test-driven development (TDD).
For additional information on related topics, take a look at the following resources:
- Dictionaries in Python (Tutorial)
- How to Iterate Through a Dictionary in Python (Tutorial)
- Custom Python Dictionaries: Inheriting From dict vs UserDict (Tutorial)
- Sets in Python (Tutorial)
- Common Python Data Structures (Guide) (Tutorial)
- Build a Hash Table in Python With TDD (Quiz)
- Using Dictionaries in Python (Course)
- Dictionaries in Python (Quiz)
- Python Dictionary Iteration: Advanced Tips & Tricks (Course)
- Python Dictionary Iteration (Quiz)
- Using Sets in Python (Course)
- Python Sets (Quiz)
- Records and Sets: Selecting the Ideal Data Structure (Course)
- Stacks and Queues: Selecting the Ideal Data Structure (Course)
- Dictionaries and Arrays: Selecting the Ideal Data Structure (Course)
- Common Python Data Structures (Guide) (Quiz)
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.