Donโt worry, you're not doing something fundamentally wrong. It's actually Python protecting you from a common programming pitfall.
The TypeError: unhashable type: 'list'
error means you attempted to use a mutable list as a dictionary key or set element. Since lists can change after creation, Python can't generate a stable hash value for them, making them unsuitable for these use cases.
The solution is straightforward: convert your list to a tuple or choose a different data structure entirely.
What Does "Unhashable Type" Mean?
Before diving into different solutions, let's understand what "unhashable" actually means in Python. And why does Python care so much about whether something can be hashed?
Hashable objects are those that have a hash value that never changes during their lifetime. Think of a hash as a unique fingerprint for an object. Python uses these hash values for quick lookups in dictionaries and sets.
Unhashable objects are mutable (changeable) and therefore can't have a consistent hash value. Since their contents can change, their "fingerprint" would change too, making them unsuitable as dictionary keys or set elements.
Common Hashable vs Unhashable Types
โ Hashable | โ Unhashable |
int, float, str
|
list
|
tuple
|
dict
|
frozenset
|
set
|
bool
|
Custom mutable objects |
When Does This Error Occur?
The TypeError: unhashable type: 'list'
error typically happens in these scenarios:
1. Using Lists as Dictionary Keys
# This will cause the error
my_dict = {[1, 2, 3]: 'value'} # โ Lists can't be keys
# This works
my_dict = {(1, 2, 3): 'value'} # โ
Tuples can be keys
2. Adding Lists to Sets
# This will cause the error
my_set = {[1, 2], [3, 4]} # โ Lists can't be in sets
# This works
my_set = {(1, 2), (3, 4)} # โ
Tuples can be in sets
3. Pandas Operations
import pandas as pd
# This might cause the error
df = pd.DataFrame({'A': [[1, 2], [3, 4]]})
df.drop_duplicates() # โ Can't hash lists for comparison
# Solution: convert lists to tuples first
df['A'] = df['A'].apply(tuple)
df.drop_duplicates() # โ
Works with tuples
How to Fix the Error
Solution 1: Convert Lists to Tuples
The most common fix is converting your list to a tuple using the tuple()
function:
# Before (causes error)
my_dict = {[1, 2, 3]: 'numbers'} # โ
# After (works perfectly)
my_dict = {tuple([1, 2, 3]): 'numbers'} # โ
print(my_dict) # Output: {(1, 2, 3): 'numbers'}
Solution 2: Use Tuple Literals
Instead of creating lists and converting them, create tuples directly:
# Storing location data with coordinates as keys
locations = {}
# Wrong way
coordinates = [40.7128, -74.0060] # NYC coordinates
locations[coordinates] = "New York" # โ TypeError
# Right way
coordinates = (40.7128, -74.0060) # Tuple instead
locations[coordinates] = "New York" # โ
Works perfectly
print(locations) # Output: {(40.7128, -74.006): 'New York'}
Solution 3: Convert Lists to Strings (When Appropriate)
If your list contains simple values, you might convert it to a string:
# For simple cases
my_list = [1, 2, 3]
my_dict = {str(my_list): 'value'} # โ
Works but not ideal
# Better approach
my_dict = {tuple(my_list): 'value'} # โ
Preferred
Solution 4: Use frozenset for Unordered Collections
If order doesn't matter, use frozenset
:
# Handling nested lists in data structures
data = [
[1, 2, 3],
[4, 5, 6],
[1, 2, 3] # Duplicate
]
# Remove duplicates by converting to set
unique_data = list(set(tuple(row) for row in data))
print(unique_data) # Output: [(1, 2, 3), (4, 5, 6)]
# Or use frozenset when order doesn't matter
my_list = [1, 2, 3, 2, 1]
my_dict = {frozenset(my_list): 'unique_values'} # โ
print(my_dict) # Output: {frozenset({1, 2, 3}): 'unique_values'}
Solution 5: Pandas-Specific Solutions
For pandas DataFrames with list columns:
import pandas as pd
# Problem DataFrame
df = pd.DataFrame({
'id': [1, 2, 3],
'tags': [['python', 'programming'], ['data', 'science'], ['python', 'programming']]
})
# Solution 1: Convert to tuples
df['tags'] = df['tags'].apply(tuple)
unique_df = df.drop_duplicates() # โ
Now works
# Solution 2: Convert to strings (if needed)
df['tags_str'] = df['tags'].astype(str)
unique_df = df.drop_duplicates(subset=['tags_str']) # โ
Alternative approach
Best Practices to Avoid This Error
- Think about mutability: If you need to use something as a dictionary key or set element, ask yourself if it needs to be mutable. If not, use a tuple instead of a list.
- Use appropriate data structures: Choose your data structures based on how you'll use them:
- Need to modify contents? Use
list
- Need as dictionary key? Use
tuple
- Need unique, unordered items? Use
set
orfrozenset
- Convert at the source: Instead of converting lists to tuples later, consider creating tuples from the start when you know you'll need them as keys.
- Handle pandas DataFrames carefully: When working with columns containing lists, convert them to tuples before operations like
drop_duplicates()
orgroupby()
.
Related Errors You Might See
You might also encounter these related errors:
TypeError: unhashable type: 'dict'
TypeError: unhashable type: 'set'
TypeError: unhashable type: 'Series'
(in pandas)
The solutions are similar: convert the unhashable type to a hashable equivalent or restructure your code to avoid using mutable objects as keys.
Putting It All Together
The TypeError: unhashable type: 'list'
error occurs because Python can't use mutable objects like lists as dictionary keys or set elements. The solution is almost always to convert lists to tuples using tuple()
, or to restructure your code to use hashable data types from the start.
When you encounter this error, here's how to debug it quickly:
- Identify the line: Look at the traceback to find exactly where the error occurs
- Check dictionary keys: Look for any dictionary assignments using square brackets
[]
- Examine set operations: Check if you're adding lists to sets
- Review pandas operations: Look for operations that need to compare or hash rows
Remember: if you need to use something as a dictionary key or put it in a set, it needs to be hashable. When in doubt, tuples are your friend!
Track, Analyze and Manage Errors With Rollbar

Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you to proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing Java errors easier than ever. Sign Up Today!