TypeError: Unhashable Type: 'list' โ How to Fix It
Answer
This error means you're trying to use a list where Python expects a hashable type โ usually as a dictionary key, in a set, or in a pandas groupby/merge operation. Fix it by converting the list to a tuple, or by restructuring your data so columns don't contain lists.
Why This Happens
Python requires dictionary keys, set elements, and groupby columns to be hashable (immutable). Lists are mutable and therefore unhashable. This commonly happens when a dataframe column contains lists as values and you try to groupby, merge, or dedupe on that column.
Solution
The rule: if you need to group, merge, or dedupe on a column with lists, convert to tuples or strings first.
import pandas as pd
# โ Problematic: list as dictionary key
my_dict = {[1, 2]: 'value'}
# TypeError: unhashable type: 'list'
# โ
Fixed: use tuple instead
my_dict = {(1, 2): 'value'}
# โ Problematic: groupby on column containing lists
df = pd.DataFrame({'tags': [['a', 'b'], ['a', 'c'], ['a', 'b']], 'value': [1, 2, 3]})
df.groupby('tags').sum()
# TypeError: unhashable type: 'list'
# โ
Fixed: convert lists to tuples for groupby
df['tags_tuple'] = df['tags'].apply(tuple)
df.groupby('tags_tuple').sum()
# โ
Alternative: convert lists to strings
df['tags_str'] = df['tags'].apply(lambda x: ','.join(x))
df.groupby('tags_str').sum()Better Workflow
Zerve displays column dtypes inline, so you can spot columns containing lists or other complex types before they break downstream operations.
)
&w=1200&q=75)
&w=1200&q=75)
&w=1200&q=75)