Fix TypeError: unhashable type: ‘list’ in Python

Quick answer: A list is unhashable because it is mutable. Do not use it as a dictionary key or set member; convert it to a tuple or frozenset only when the immutable representation matches the data’s intended equality and ordering semantics.

Python unhashable list infographic comparing mutable lists with tuple and frozenset dictionary keys
Use tuple or frozenset only when the collection should be immutable and its equality semantics match the key you need.

TypeError: unhashable type: 'list' means Python tried to use a list in a place that requires a hashable object. The most common causes are using a list as a dictionary key, putting a list inside a set, or using a tuple that contains a list as a key.

The direct fix is usually simple: if the sequence must be a key, convert it to an immutable tuple; if the sequence is just data, keep it as a dictionary value instead. The official Python glossary defines a hashable object as one with a hash value that does not change during its lifetime and can be compared to other objects.

Why the Error Happens

Dictionaries and sets use hash values internally. Python’s dictionary documentation explains that dictionary keys must be hashable. Lists are mutable, so Python does not allow them as keys.

lookup = {
    ["python", "pandas"]: "tutorials"
}

print(lookup)

Output:

TypeError: unhashable type: 'list'

This fails because ["python", "pandas"] is a list. Python would not be able to rely on its hash if the list changed after being inserted into the dictionary.

Fix 1: Convert the List to a Tuple

If the list represents one key, use a tuple instead. A tuple can be used as a dictionary key when all of its items are hashable.

lookup = {
    ("python", "pandas"): "tutorials"
}

print(lookup[("python", "pandas")])

Output:

tutorials

Use this fix when the order and exact combination of values identify something. If you need to learn more tuple patterns, Python Pool has a guide to unpacking tuples in Python.

Python Pool infographic showing dictionary, list key, hash table, unhashable error, and required immutable key
Lists are mutable, so Python cannot use them as dictionary keys or set members.

Fix 2: Keep the List as a Dictionary Value

Sometimes the list should not be the key at all. In that case, give the data a string key and store the list as the value.

country = {
    "name": "USA",
    "states_and_rank": [50, 4]
}

print(country["states_and_rank"])

Output:

[50, 4]

This is usually the cleanest design for structured data. For deeper dictionary structures, see nested dictionaries in Python and Python dictionary size.

Fix 3: Convert Lists Before Adding Them to a Set

The same error happens with sets because set elements must be hashable. Python’s set documentation describes sets as unordered collections of distinct hashable objects.

rows = [[1, 2], [1, 2], [3, 4]]
unique_rows = {tuple(row) for row in rows}

print(unique_rows)

Output:

{(1, 2), (3, 4)}

After deduplicating, convert the rows back to lists only if you need mutable lists again:

deduped_lists = [list(row) for row in unique_rows]
print(deduped_lists)
Python Pool infographic comparing mutable list, immutable tuple, hash, dictionary key, and successful lookup
Use a tuple only when all of its elements are hashable and the key should be immutable.

Tuple Does Not Fix Every Case

A tuple is hashable only when each item inside it is hashable. A tuple that contains a list still raises the same error.

bad_key = ([1, 2], "python")

lookup = {
    bad_key: "value"
}

Fix nested mutable values too:

good_key = ((1, 2), "python")

lookup = {
    good_key: "value"
}

print(lookup[good_key])
Python Pool infographic mapping nested list through recursive conversion to tuple and hashable structure
Nested lists must be converted at every level if the full structure is intended to become a key.

Quick Checklist

  • Use a tuple when the list-like data must act as a key.
  • Use a string or number key when the list is only stored data.
  • Convert nested lists too; a tuple containing a list is still unhashable.
  • Use tuples before adding list-like rows to a set.

Why Mutability Matters

A dictionary or set relies on a stable hash while it stores a key or member. If a list could change after insertion, its lookup position could become invalid. Python therefore rejects lists in these hash-based containers instead of guessing how mutation should work.

try:
    lookup = {["python", "pool"]: "guide"}
except TypeError as error:
    print(error)
Python Pool infographic testing equality, mutation, set membership, dictionary keys, and validation
Check whether the object is mutable, where it is used, and whether a stable immutable representation is appropriate.

Choose tuple Or frozenset

Use tuple when order and duplicates are meaningful. Use frozenset when order should not matter and duplicate values can collapse. Both choices change the data model, so document the conversion at the boundary rather than scattering tuple() calls through business logic.

ordered_key = tuple(["python", "pool"])
unordered_key = frozenset(["python", "pool"])
lookup = {ordered_key: "ordered", unordered_key: "unordered"}
print(lookup[ordered_key])

Do Not Mutate A Key

Making a tuple hashable does not make every object inside it safe. A tuple containing a list is still unhashable, while a tuple of immutable values is suitable when its contents are stable. Validate nested values before using a converted key.

safe = ("python", 3, ("seo", "guide"))
print(hash(safe))

try:
    print(hash(("python", ["guide"])))
except TypeError as error:
    print(error)

Python’s glossary defines hashable objects; use that contract rather than treating tuple conversion as a universal fix.

When a collection crosses an API or persistence boundary, define whether order, duplicates, and nested values are part of identity. Test the chosen representation with empty input, repeated values, and attempted mutation so the key policy remains explicit and stable. Also document why tuple or frozenset was selected for future callers and reviewers.

For related collection choices, compare converting a list to a set, copying a list, and combining lists.

Frequently Asked Questions

Why is a list unhashable in Python?

Lists are mutable, so their contents can change after insertion; mutable values are not suitable for stable set membership or dictionary hashing.

How do I use a list as a dictionary key?

Convert it to a tuple when order matters, or use a frozenset when order should not matter and duplicate semantics are acceptable.

Can I put a list inside a set?

No. Use an immutable representation such as a tuple, or redesign the set value if mutability is part of the data model.

Is converting every list to a tuple always correct?

No. Conversion changes the data’s mutability and possibly its meaning; use it only when the key or set member should be immutable.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted