A hash table is a general data structure that maps keys to values by hashing the key to find a slot. A Python dict is the language's built-in, ready-to-use mapping type, and CPython implements it with a highly tuned hash table. So a dictionary is a hash table in practice, but a hash table is the idea and dict is one specific implementation with extra guarantees such as insertion order.
You almost never need to write your own hash table in Python. This article explains how dict works, what it adds on top of the textbook structure, and when a hand-written table makes sense.
| Aspect | Hash table (concept) | Python dict |
|---|---|---|
| What it is | An abstract data structure with many designs | A concrete built-in type |
| Collision handling | Chaining or open addressing, your choice | Open addressing with probing, handled for you |
| Ordering | Usually unordered | Keeps insertion order (guaranteed since Python 3.7) |
| Resizing | You implement it | Automatic |
| Keys | Whatever your hash function supports | Must be hashable (__hash__ and __eq__) |
| Average lookup, insert, delete | O(1) | O(1) |
| Worst case | O(n) | O(n), rare |
Other languages use the word differently: Java has HashMap, C++ has std::unordered_map and Python calls its mapping a dictionary. The terms are often used interchangeably in conversation, which is the source of the confusion.
Since Python 3.6 CPython uses a compact layout. It keeps a dense array of entries (hash, key, value) in insertion order and a small sparse index array that points into it. Lookup hashes the key, probes the index array using a pseudo-random probe sequence, and compares candidates first by hash and then by equality. The table resizes when it becomes about two thirds full, which keeps probes short.
This design saves memory compared with the older layout and is why iteration follows insertion order. The language guarantee of ordered dictionaries was added in Python 3.7.
You can see hashing at work:
The tuple hash value and the exact error wording differ between runs and Python versions, and string hashes are randomized per process for security, so never store or compare hashes across processes. Mutable built-ins such as list, dict and set are unhashable because their contents can change, which would corrupt the table.
Dictionaries give you the operations of a hash table with a concise syntax and extra helpers.
Useful standard-library companions are collections.defaultdict for default values, collections.Counter for counting, and collections.OrderedDict only when you need order-sensitive equality or move_to_end.
Building one is a good learning exercise and shows what dict hides. This version uses separate chaining and resizes when the load factor passes 0.75.
This works, but it is slower and uses more memory than the built-in dict, which is written in C, and it has no ordering or deletion. Use a plain dict in real code.
set when you only need keys and fast membership tests, since it is also a hash table.dict with frozenset or tuple keys for composite keys.sortedcontainers.SortedDict (third-party) or the bisect module when you need sorted keys or range queries, because hash tables have no key ordering by key value.functools.lru_cache or functools.cache instead of a hand-written memoization table.dataclasses or typing.TypedDict when the keys are fixed fields rather than arbitrary data.Yes. CPython's dict is a hash table using open addressing with a compact entries array. The language only promises the behavior, such as average O(1) operations and insertion order, so other implementations may differ internally.
Yes. Iteration follows insertion order, guaranteed by the language since Python 3.7. Updating the value of an existing key does not change its position.
Lists are mutable and unhashable. Convert it to a tuple, or a frozenset if order does not matter, to use it as a key.
That is a collision. CPython probes other slots and compares keys with ==, so both keys are stored correctly, with a small cost in time.
A hash table is the underlying idea and a Python dict is its optimized, ordered, built-in implementation with automatic resizing and collision handling. Use dict and set for almost every need, and write your own table only to learn or for a very specific requirement. Remember that keys must be hashable and that hashes are not stable across processes.