How to Fix: TypeError: unhashable type: ‘list’

3D illustration of trying to use a flexible chain as a key for a lock, representing the unhashable type list error.

This error is a core Python concept. The TypeError unhashable type message means you tried to use something “changeable” (mutable) in a place that requires something “unchangeable” (immutable).

  • Hashable (Immutable): Can be “locked” into a unique ID. Examples: str, int, tuple.
  • Unhashable (Mutable): Can be changed in-place. Examples: list, dict.

The Rule: Dictionary keys and Set items MUST be hashable.

⚡ Quick Fix: TypeError: unhashable type: ‘list’ — Python tuple() Conversion Fix for Dictionary Keys, Set Items, and Pandas GroupBy Operations

You put a list where Python requires a hashable type — dictionary keys and set members must be immutable so Python can compute a fixed hash value for them.

# WRONG — lists are mutable, Python can't hash them as dict keys
my_list = [1, 2]
my_dict = {my_list: "value"}      # TypeError: unhashable type: 'list'

# WRONG — sets only store hashable items
my_set = {[1, 2], [3, 4]}         # TypeError: unhashable type: 'list'

# WRONG — Pandas groupby with a list column crashes the same way
df.groupby(['col_a', ['col_b', 'col_c']])  # nested list triggers unhashable error

# RIGHT — convert list to tuple: tuples are immutable and hashable
my_tuple = (1, 2)
my_dict = {my_tuple: "value"}     # works — tuple is a valid dict key
print(my_dict[(1, 2)])            # Output: value

# RIGHT — tuples inside sets
my_set = {(1, 2), (3, 4)}        # works

# RIGHT — frozenset for cases where you need a hashable set
my_dict = {frozenset([1, 2]): "value"}   # frozenset is immutable and hashable

The two causes below cover dictionary keys and set membership — with the Pandas groupby scenario that catches data engineers off guard.

The Cause 1: Dictionaries

You cannot use a list as a dictionary key. Python doesn’t allow it because if you changed the list later, the dictionary would break.

Problem Code:

my_list = [1, 2]
my_dict = {
    my_list: "My Value" # CRASH!
}
# TypeError: unhashable type: 'list'

The Fix: Use an immutable tuple instead. Tuples look just like lists but use () and cannot be changed.

my_tuple = (1, 2)
my_dict = {
    my_tuple: "My Value" # Works!
}
print(my_dict[(1, 2)]) # Output: My Value

The Cause 2: Sets

Sets are for storing unique, hashable items. You can’t put a list inside a set. Problem Code:

my_set = { [1, 2], [3, 4] } # CRASH!

The Fix: Use tuples.

my_set = { (1, 2), (3, 4) } # Works!

TypeError: unhashable type: ‘list’ — The Hashability Rule and Three Conversions That Fix It

TypeError: unhashable type: ‘list’ enforces one contract: anything used as a dictionary key or set member must have a fixed, stable identity Python can compute once and trust forever. Lists fail because their content can change after creation — a list with [1, 2] today might be [1, 2, 3] tomorrow, which would silently break the dictionary’s internal lookup table.

Three conversions fix this permanently depending on your use case.

Convert list to tuple. tuple([1, 2]) gives (1, 2) — identical content, immutable wrapper, fully hashable. Use this for dictionary keys built from lists of fixed values: coordinate pairs, composite keys, multi-column group labels.

Convert list to frozenset when order doesn’t matter and uniqueness does. frozenset([1, 2, 2]) gives frozenset({1, 2}) — an immutable, unordered, deduplicated set that works as a dict key or set member. Use this when you need to represent a group of items where the sequence is irrelevant.

Use tuple(sorted(…)) for canonical keys built from unordered data. Two lists [1, 2] and [2, 1] produce different tuples but the same frozenset. If your key should treat both as equal, tuple(sorted(my_list)) normalises the order before hashing.

Canonical key pattern

key = tuple(sorted(my_list))
my_dict[key] = “value”

The Pandas case worth knowing: df.groupby() accepts a list of column names, not a list containing a list. df.groupby([‘col_a’, ‘col_b’]) works. df.groupby([‘col_a’, [‘col_b’, ‘col_c’]]) fires TypeError: unhashable type: ‘list’. Flatten the column names into a single list — no nesting.

Similar Posts

Leave a Reply