Back to Python & Coding

PYTHON & CODING · QUESTION 82

What makes an object hashable in Python?

On this page
Intermediate 4 min read
Interview-ready answerThe concise version to remember

A hashable object has a hash value that remains stable during its lifetime and can be compared for equality. Equal objects must have equal hashes. Dictionaries and sets use hashes to find candidate entries, then equality checks to resolve collisions. Immutable built-in values such as strings and integers are hashable; mutable containers such as lists are not.

LET'S MAKE IT SIMPLE

First, the idea in plain English

A dictionary needs a quick way to narrow down where a key might be stored. It computes a hash number from the key, then checks equality when needed. If a key changed its hash after insertion, the dictionary might search the wrong place, which is why mutable keys are generally unsafe.

A USEFUL COMPARISON

Picture it this way

A library assigns each book a catalog code and still checks the title to distinguish books that share a shelf. A hash is like the code: it speeds lookup but does not prove two objects are equal.

FOLLOW THE JOURNEY

Hash selects a lookup area; equality confirms the key

Show hash computation, bucket lookup, collision handling, and key invariants.

Hash selects a lookup area; equality confirms the keyShow hash computation, bucket lookup, collision handling, and key invariants.Data-structure lookup
01Compute hashApply the key’s hash function to obtain an integer.
02Find candidate entriesUse the hash to inspect a table location or probe sequence.
03Check equalityCompare candidate keys to identify the exact match.
04Preserve stabilityKeep equality and hash behavior consistent while the key is stored.

This simplified visual highlights the main relationships. In practice, the exact design depends on the requirements; Changing a key’s hash can make it unreachable.

A dictionary hashes a key to select a bucket, compares keys for equality when collisions occur, and relies on a stable hash for stored keys.

How to read this visual
This simplified visual highlights the main relationships. In practice, the exact design depends on the requirements; Changing a key’s hash can make it unreachable.

UNDER THE HOOD

How it works, step by step

01

Compute hash

Apply the key’s hash function to obtain an integer.

Why it matters: The hash narrows the lookup search.

02

Find candidate entries

Use the hash to inspect a table location or probe sequence.

Why it matters: Different keys can still collide.

03

Check equality

Compare candidate keys to identify the exact match.

Why it matters: Hash equality alone does not establish key equality.

04

Preserve stability

Keep equality and hash behavior consistent while the key is stored.

Why it matters: Changing a key’s hash can make it unreachable.

OUTSIDE THE TEXTBOOK

A practical example

Strings can be dictionary keys for counting words because their hash and equality behavior are stable. A list of tags cannot be used directly as a key; converting it to a tuple is possible when the values inside are themselves hashable and order is meaningful.

GO A LITTLE DEEPER

Technical details that matter

  • Python requires `a == b` to imply `hash(a) == hash(b)` for hashable objects.
  • A custom class that defines equality but not a compatible hash may become unhashable.
  • Hash randomization and table resizing are implementation details; code should not rely on iteration order as a hash-table property.
Python example
counts = {}
for word in ["tea", "tea", "coffee"]:
    counts[word] = counts.get(word, 0) + 1

print(counts["tea"])

Strings are hashable dictionary keys. The dictionary uses the key hash for lookup and equality to distinguish entries; the example prints 2.

MAKE A GOOD CHOICE

When is this useful?

  • Choosing keys for dictionaries and members for sets.
  • Implementing custom value objects used in caches or indexes.
  • Debugging TypeError messages about unhashable types.
Consider the alternative

Use an immutable tuple or a frozen data class when a compound key is needed. Choose a stable identifier if the object itself is mutable.

WATCH OUT FOR

Common misunderstandings

  • Assuming a unique hash means objects are equal.
  • Using mutable state in a custom object’s hash.
  • Defining `__eq__` without ensuring compatible hash behavior.

REAL-WORLD TRADE-OFFS

Limitations and failure cases

  • Hash collisions are possible and equality checks still matter.
  • Hashability does not imply global uniqueness or security.
  • Custom hashes must remain stable for the object’s lifetime.

WHAT TO REMEMBER

Key takeaways

  1. 1Hashable keys need stable hashes and equality behavior.
  2. 2Equal values must have equal hashes.
  3. 3Dictionaries use hashes to narrow lookup and equality to confirm.

PRACTICE THE FOLLOW-UP

What might an interviewer ask next?

Can two different objects have the same hash?

Yes. Hash collisions are expected; dictionaries resolve them by checking equality among candidate keys.

Why are lists unhashable?

Lists are mutable, so their contents and derived hash could change after insertion. Python prevents this unsafe key behavior.

SAY IT OUT LOUD

How to explain this in an interview

“A hashable object has a stable hash and equality behavior; equal objects must have equal hashes. Dictionaries use the hash to locate candidates and equality to resolve collisions. Immutable values such as strings are usually safe keys, while mutable lists are not. For custom objects, I make sure equality and hash use consistent, stable fields.”

Continue your learning path

Connect this question to the roadmap topic it supports.

Open roadmap topic