Content · Programming · Python

Python Dictionary Key Requirements: Hashable Objects

A dictionary key must be hashable so its hash value can be used in lookup. Strings, integers and immutable tuples of hashable contents work; lists and mutable dictionaries do not. See two practical examples, edge cases.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

A grid maps fixed coordinate pairs to stored values.

What You Need to Understand


A dictionary key must be hashable so its hash value can be used in lookup. Strings, integers and immutable tuples of hashable contents work; lists and mutable dictionaries do not.

When to Use It


A grid maps fixed coordinate pairs to stored values.

Step-by-Step Python Example


cells = {(0, 0): "empty", (0, 1): "wall"}
print(cells[(0, 1)])
print(hash((0, 1)))

How the Example Works


A tuple of integers can act as one coordinate key. Its hash can be calculated because its members are hashable.

Example Output


wall
-1950498447580522560
team

A Second Example


Compare this smaller variation with the main example, then experiment with your own values.

groups = {frozenset({"a", "b"}): "team"}
print(groups[frozenset({"b", "a"})])

Important Behavior and Edge Cases


Hashability is not identical to immutability of every nested object, and equal keys must have equal hashes. (1, 2) works, but ([1], 2) cannot be hashed.

Try It Yourself


Try using an immutable frozenset as a dictionary key.

Common Beginner Mistakes


  • Hashability is not identical to immutability of every nested object, and equal keys must have equal hashes. (1, 2) works, but ([1], 2) cannot be hashed.
  • Treating missing dictionary keys, replacement precedence, and shallow copies as if they had identical behavior in every context.

Key Takeaways


  • A dictionary key must be hashable so its hash value can be used in lookup.
  • A tuple of integers can act as one coordinate key.
  • Hashability is not identical to immutability of every nested object, and equal keys must have equal hashes.
Share this resourceSend this page to someone who may find it useful.