Last modified: Apr 09, 2025 By Alexander Williams
Fix TypeError: unhashable type 'list' in Python
Python developers often encounter the error TypeError: unhashable type: 'list'. This error occurs when you try to use a list as a key in a dictionary or as an element in a set.
Table Of Contents
- Why Does This Error Occur?
- Common Scenarios That Cause This Error
- Example 1: List as Dictionary Key
- Solution 1: Convert List to Tuple
- Example 2: Adding List to Set
- Solution 2: Use Frozenset or Tuple
- Example 3: Using List in Built-in Functions
- Solution 3: Convert Nested Lists to Tuples
- Best Practices to Avoid This Error
- When to Use Lists vs Tuples
- Conclusion
Why Does This Error Occur?
In Python, only immutable objects can be used as dictionary keys or set elements. Lists are mutable, meaning they can be changed after creation. This makes them unhashable.
Hashing is a process Python uses to quickly compare dictionary keys or set members. Since lists can change, Python can't reliably hash them.
Common Scenarios That Cause This Error
Here are three common situations where you might see this error:
- Using a list as a dictionary key
- Adding a list to a set
- Using a list in a function that requires hashable types
Example 1: List as Dictionary Key
This code tries to use a list as a dictionary key, which causes the error:
# This will raise TypeError
my_dict = {}
key_list = [1, 2, 3]
my_dict[key_list] = "value" # Error occurs here
TypeError: unhashable type: 'list'
Solution 1: Convert List to Tuple
Since tuples are immutable, they can be used as dictionary keys. Convert your list to a tuple first:
my_dict = {}
key_list = [1, 2, 3]
my_dict[tuple(key_list)] = "value" # This works
print(my_dict)
{(1, 2, 3): 'value'}
Example 2: Adding List to Set
Sets require all elements to be hashable. This code fails when trying to add a list:
my_set = set()
my_list = [4, 5, 6]
my_set.add(my_list) # Error occurs here
TypeError: unhashable type: 'list'
Solution 2: Use Frozenset or Tuple
Convert the list to a frozenset or tuple before adding to the set:
my_set = set()
my_list = [4, 5, 6]
my_set.add(frozenset(my_list)) # This works
print(my_set)
{frozenset({4, 5, 6})}
Example 3: Using List in Built-in Functions
Some Python functions like set()
or dict.fromkeys()
require hashable elements:
nested_lists = [[1, 2], [3, 4]]
unique_lists = set(nested_lists) # Error occurs here
TypeError: unhashable type: 'list'
Solution 3: Convert Nested Lists to Tuples
First convert the inner lists to tuples, then create the set:
nested_lists = [[1, 2], [3, 4]]
unique_tuples = {tuple(inner) for inner in nested_lists}
print(unique_tuples)
{(1, 2), (3, 4)}
Best Practices to Avoid This Error
Follow these tips to prevent unhashable type errors:
- Always use immutable types as dictionary keys
- Convert lists to tuples when you need hashable versions
- Check variable types before using them in sets or as keys
- Use
type()
orisinstance()
to verify types
When to Use Lists vs Tuples
Understanding when to use lists versus tuples can help avoid this error. Lists are for mutable sequences, while tuples are for immutable sequences.
If you need to modify the sequence, use a list. If you need a hashable sequence, use a tuple. For more Python error solutions, see our guide on How To Solve ModuleNotFoundError.
Conclusion
The TypeError: unhashable type: 'list' occurs when you try to use a list where Python expects a hashable type. The solution is usually to convert the list to a tuple or frozenset.
Remember that dictionary keys and set elements must be immutable. By understanding this fundamental Python concept, you can avoid this common error in your code.
For more complex data structures, consider using custom classes with properly implemented __hash__
methods. Always test your code with different input types to catch these issues early.