Error guide · TypeError
TypeError: unhashable type: 'list'
You used a list where Python needs a value that cannot change, such as a dictionary key.
Example
seen = {}
seen[[1, 2]] = "pair"
print(seen)What Python says
TypeError: unhashable type: 'list'
As printed by Python 3.12. Other versions may word it slightly differently.
Why it happens
Dictionary keys and set items must be unchangeable so that Python can file them reliably. Numbers, text and tuples qualify. Lists do not, because they can be changed after they are stored. Use a tuple instead.
How to fix it
seen = {}
seen[(1, 2)] = "pair"
print(seen)Output: {(1, 2): 'pair'}
Tips
- Turn a list into a tuple with
tuple(my_list)when you need it as a key. - The same message appears for
{[1, 2]}, because sets follow the same rule. - A set or a dictionary cannot be a key either.