Write Python. Understand every error.

Open the editor

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.

Try this code in Py-CRAFT

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

Learn the ideas behind it

Other common errors

Share this page: