TypeError: unhashable type: 'list'
TypeError: unhashable type: 'list' in Python means you used a list where Python needs a value that can never change: in a set, as a dictionary key, or in a set or dict lookup. Use a tuple instead.
How do I fix TypeError: unhashable type: 'list'?
TypeError: unhashable type: 'list' means you put a list somewhere Python needs a fixed value: in a set, as a dictionary key, or in a set or dict lookup. Lists can change, so they cannot be hashed. Convert the list to a tuple first, for example seen.add((x, y)) or {tuple(p) for p in pairs}.
- 1. Find the hashing callThe failing line uses set(), .add(), d[key] or `in` on a set or dict. That is where the list goes in.
- 2. Swap brackets for a tupleWrite (x, y) instead of [x, y], or wrap an existing list with tuple(value) before storing it.
- 3. Lists inside a listset(pairs) fails when each pair is a list. Use {tuple(p) for p in pairs} to remove duplicates.
What does TypeError: unhashable type: 'list' mean?
Sets and dictionaries find items by their hash, and a hash is only valid while the value stays the same. Lists can be changed, so Python refuses to hash them. Python 3.14 adds context, such as "cannot use 'list' as a set element". Tuples, strings and numbers are hashable; lists, dicts and sets are not.
Why does TypeError: unhashable type: 'list' happen?
1. Adding a list to a set
Common when collecting coordinates or pairs.
Breaks
seen = set()
seen.add([x, y])Works
seen = set()
seen.add((x, y))2. Using a list as a dictionary key
Keys must be hashable for the same reason.
Breaks
visits = {}
visits[[lat, lng]] = 1Works
visits = {}
visits[(lat, lng)] = 13. Removing duplicates from a list of lists
set() has to hash every element, and the elements are lists.
Breaks
unique = set(pairs)Works
unique = {tuple(p) for p in pairs}How to find it in your own code
The failing line is the one that hashes: set(), .add(), a dictionary key, or `in` on a set or dict. Convert the value with tuple(value) before storing it, and back with list() only if you need to change it later.
Try it: fix this bug
mediumcount_unique_points([[0, 0], [1, 2], [0, 0]]) crashes: TypeError: cannot use 'list' as a set element (unhashable type: 'list').
def count_unique_points(points):
seen = set()
for p in points:
seen.add(p)
return len(seen)Still not sure why yours breaks?
Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.