FULL PYTHON FIX · 8 MIN READ
How to Fix TypeError: unhashable type: 'list' in Python
A list cannot be a set member or dictionary key because it is mutable and unhashable. Convert fixed coordinate values to tuples, then test duplicates and nested values.
Video guide: How to Fix TypeError: unhashable type: 'list' in Python
The preview is stored on this site. YouTube loads only when you press Play. Watch on YouTube
Jump to a chapter
Read the video transcript
Select a timestamp to watch that moment on YouTube.
- 00:00
I have a coordinate with x equal to three and y equal to five. I want to remember which coordinates a program has visited, so I create a set and add the point. Python stops with TypeError: unhashable type list. The numbers are fine. The problem is the type of the object I am trying to use as a set member. Let us reproduce the error before changing the data. A dictionary key has a similar requirement, so the same message can appear when you try to write a mapping from a list to a label. The fix is not simply to silence the exception. We need to represent a fixed coordinate in a way that a set or dictionary can safely use as a key.
- 00:42
We will inspect why a list does not qualify, convert each two-integer coordinate to a tuple, and then verify duplicate removal without losing the order in which points first appeared. The first example proves an important distinction. A dictionary value can be a list. Here the key is the string start and the value is a coordinate list, so the mapping works. The error concerns members of a set or keys of a dictionary. Those containers use a hash to locate an item. A hashable object needs a hash value that remains stable and equality that agrees with it. A list can be edited after creation: you can append to it or replace one of its elements.
- 01:25
Python does not allow such a list to be used directly as a hash key. The second run shows another trap. I wrapped the list in a tuple, but the tuple still contains a list. Hashing the tuple tries to account for its elements and fails on the inner list. So the advice to convert a list to a tuple is conditional: the values inside the tuple must themselves be hashable. In our coordinate example, both elements are integers, so that condition holds. For each coordinate list, tuple of point creates a tuple containing the same two integers. That tuple is a stable key suitable for our set. Three input points produce only two distinct set members because the first and third coordinates are equal.
- 02:11
Membership confirms that three, five is among them. Notice that the original lists still exist; tuple(point) made a new representation of each current coordinate. If I later edit an original list, the tuple already in the set does not change with it. That is usually exactly what a visited-coordinate key should mean, but it is important to know which value was stored. If you need an editable working coordinate, keep the list for editing and derive a tuple when you need a key. Do not convert an arbitrary nested structure without defining what the immutable key should represent. A string representation may appear to work, but then lookups depend on formatting rather than a clear data model.
- 02:56
The set is good for membership checks, but it is not the right output structure when the order of first appearance matters. The unique list records that order, while seen prevents a duplicate from being appended. The first point is three, five; the next new point is one, two. The repeated three, five is skipped. This version also handles an empty input naturally: the loop does nothing and the output is an empty list. The second run reminds us of the limit of a shallow conversion. The outer list becomes a tuple, but the inner list remains mutable and unhashable. If nested coordinates are valid in your application, define and test a complete immutable representation, perhaps a tuple of tuples.
- 03:41
That is a separate data-design decision. For this guide, the contract is a flat coordinate list containing two integers. Your practice task is to write unique_points. It takes a sequence such as three, five; one, two; three, five, with each point supplied as a two-integer list. Return a new list containing distinct tuples in first-seen order, and leave the input unchanged. Test duplicates, one point, and an empty list. The checked task will catch a solution that returns a set or changes the caller's data. After that, change the first input list from three, five to three, six and print the returned tuples again.
- 04:22
The saved tuple still contains three, five, because it captured the old integer values. This small experiment separates the editable source list from the immutable key created for a lookup. Also test two different list objects that each contain three and five. Their tuple keys compare equal, so the second point should be treated as a duplicate even though the original lists are separate objects. When you encounter unhashable type list, first ask where the list is being used. A list as a dictionary value is fine. A list as a set member or dictionary key is not. If the value is conceptually fixed and its contents are hashable, choose a tuple representation.
- 05:05
If the value must stay editable, keep it as a list and derive an immutable key only for the lookup. The full guide links to the lists, tuples and sets, and dictionaries lessons for the underlying collection rules.
Put a coordinate list into a set
A map program stores a coordinate as a two-item list. To remove duplicate positions, you may try to put the coordinate in a set. Run the example and inspect the final traceback line. Python raises TypeError before the set can hold the first coordinate. The list contents are valid numbers; the container type is the problem.
The same error appears when a list is used as a dictionary key. It does not mean a set can never contain information that originally came from a list. It means the object chosen as an individual set member must meet the set's key requirement. Decide whether the value represents a fixed key or a collection you still intend to edit.
point = [3, 5]
visited = set()
visited.add(point)TypeError: unhashable type: 'list'point = [3, 5]
labels = {point: 'start'}TypeError: unhashable type: 'list'Understand why a mutable list is not a key
Sets and dictionaries use hashes to locate their members or keys. A hashable object has a hash value that does not change during its lifetime and supports equality in a compatible way. A list can change after creation: append, item assignment, and other operations can change its contents. Python therefore leaves lists unhashable rather than letting a mutable key break lookups.
Do not confuse a list stored as a dictionary value with a list used as a key. Values can be lists: {'route': [3, 5]} is fine because the key is the string route. Nor does calling tuple on any arbitrary nested list guarantee success. A tuple can only be a set member when all its elements are hashable. Inspect the shape of your data before choosing a conversion.
labels = {'start': [3, 5]}
print(labels['start'])
print(type(labels['start']).__name__)[3, 5]
listnested = ([3, 5], 'start')
print(hash(nested))TypeError: unhashable type: 'list'Use a tuple for each fixed coordinate
For a fixed coordinate made of integers, convert the two-item list to a tuple before adding it to the set. A tuple of integers is hashable, so the set can track it. If the same coordinate appears again, its tuple compares equal to the existing member and the set keeps one copy. The source list does not become frozen; the new tuple is a separate representation of its current values.
Choose this repair only when the list's elements are suitable key components. If you still need to edit a coordinate, keep the editable list separately and derive a tuple at the moment you need a key. Converting an arbitrary list to its string representation is usually a poor key design: formatting is not the same thing as a documented value model.
points = [[3, 5], [1, 2], [3, 5]]
visited = set()
for point in points:
visited.add(tuple(point))
print(len(visited))
print((3, 5) in visited)2
Truepoint = [3, 5]
labels = {tuple(point): 'start'}
print(labels[(3, 5)])startCheck duplicates, input order, and nested data
A set removes duplicates but does not promise the original input order. If your program must return positions in first-seen order, keep a separate result list and use the set only for membership checks. Convert each point to a tuple once, add it to seen, and append it to the result only if it was not already there. Test an empty input and multiple duplicates.
Confirm your domain before flattening or recursively converting nested values. A coordinate such as [3, [5, 7]] still contains an inner list after a simple tuple conversion and cannot be hashed. If that nested structure is expected, define a deliberate immutable representation for every level or use another data structure. The fix is about choosing a stable key, not mechanically wrapping every list in tuple().
points = [[3, 5], [1, 2], [3, 5]]
seen = set()
unique = []
for point in points:
key = tuple(point)
if key not in seen:
seen.add(key)
unique.append(key)
print(unique)[(3, 5), (1, 2)]print(hash(tuple([3, [5, 7]])))TypeError: unhashable type: 'list'- A list is mutable and cannot serve as a set member or dictionary key.
- A tuple is hashable only when every element it contains is hashable.
- Use a set for membership and a list for first-seen output order when both matter.
Practice the fix
Write unique_points(points) for a sequence of two-integer coordinate lists. Return a new list of distinct coordinate tuples in first-seen order. Do not change the input. Test duplicates, one point, and an empty list.
Need a hint?
For each point, build key = tuple(point). If key is not in a seen set, add it to seen and append key to the result list.
Python console
Ready to runEdit the code and run it in your browser. Examples above can be loaded with Try this example.
Does your code use input()? Add one value per line
Your output appears here.
Write your solution, then select Check practice.
Key takeaways
- Set members and dictionary keys must be hashable; lists are not.
- A tuple of immutable coordinate values can represent a fixed key.
- A tuple that still contains a list remains unhashable.
- Use a separate result list when duplicate removal must preserve first-seen order.