Why must dictionary keys be hashable?
Because the entry is stored in a slot chosen from the key's hash. If the key could change afterwards, its hash would change, the lookup would go to a different slot, and the entry would be unreachable. So keys must be immutable — tuples and strings qualify, lists and dicts do not.
Overview
The invariant being protected
Equal objects must have equal hashes, and an object's hash must not change while it is in use as a key. Both follow from how the table works: the hash is the address, so a moving hash is a moving address.
Python enforces this by making mutable built-ins unhashable. list, dict and set all set __hash__ = None, which is why the error is unhashable type rather than something about dictionaries.
Step through it
What to watch
- The rule follows from the storage, not from taste.
- Mutating a key would leave the entry in a slot nothing looks at.
- A tuple containing a list is also unhashable — it goes all the way down.
Say this out loud
"The key's hash decides where the entry lives. If the key could mutate, the hash would change and you could never find the entry again - so keys have to be immutable. A tuple works; a list doesn't."