This tutorial will guide you step by step on how to fix the “TypeError: unhashable type: ‘dict’” error. The problem often encountered when we use unhashable objects as keys in our dictionaries.
What causes the “TypeError: unhashable type: ‘dict’” error?
Reproduce the “TypeError: unhashable type: ‘dict’” error in Python
Example 1:
In this example, I will create a dictionary that contains the numbers of different types of animals in a zoo and try to find out which animal has a quantity of more than four.
animals = [ {"name": "zebra", "quantity": 7}, {"name": "tiger", "quantity": 4}, {"name": "cheetah", "quantity": 1}, {"name": "elephant", "quantity": 2}, {"name": "snake", "quantity": 10} ] moreThanFour = {} for i in animals: if i["quantity"] > 4: moreThanFour[i] = i["quantity"] print(moreThanFour)
Output:
Traceback (most recent call last):
File "<string>", line 13, in <module>
moreThanFour[i] = i["quantity"]
TypeError: unhashable type: 'dict'
And we have run into this error. Here is the reason:
If we take a look at line 13:
moreThanFour[i] = i["quantity"]
Our program will run this code in the if statement:
moreThanFour[{"name": "zebra", "quantity": 7}] = 7
This means that we are trying to use a dictionary as a key in a dictionary.
The dictionary in Python has the keys and the values. The keys have to be hashable. In Python, only immutable objects: integer, string, float, boolean, unicode, and tuple are hashable. Dictionary is not a hashable object, so it could not be used as a key in a dictionary.
How to solve the error
Solution 1: Use a valid key for the dictionary
For example 1, we can use i[“name”] as the key for our dictionary.
Fix code for example 1:
animals = [ {"name": "zebra", "quantity": 7}, {"name": "tiger", "quantity": 4}, {"name": "cheetah", "quantity": 1}, {"name": "elephant", "quantity": 2}, {"name": "snake", "quantity": 10} ] moreThanFour = {} for i in animals: if i["quantity"] > 4: moreThanFour[i["name"]] = i["quantity"] print(moreThanFour)
Output:
{'zebra': 7, 'snake': 10}
Solution 2: Convert the dictionary into a hashable object
Let’s look at another example that also runs into the same error.
Example 2:
animals = { "tall": "giraffe", "big": "hippo" } dictionary = { animals: "elephant" } print(dictionary)
Output:
Traceback (most recent call last):
File "<string>", line 6, in <module>
dictionary = {
TypeError: unhashable type: 'dict'
Again, animals is a dictionary. And since the dictionary is not a hashable object, when we use animals as a key, we will run into the same error.
We can fix it by converting the dictionary into a hash object like a tuple or frozenset. Using:
or
- iterable: an iterable.
Fixed code for example 2:
animals = { "tall": "giraffe", "big": "hippo", } dictionary = { tuple(animals): "elephant" } print(dictionary)
Output:
{('tall', 'big'): 'elephant'}
Summary
“TypeError: unhashable type: ‘dict’” is a common error in Python. It often occurs when we try to use an unhashable object as a key in our dictionary. Therefore, to avoid this problem, make sure that our keys are immutable objects or, if not, convert them into hashable objects like strings, integers, tuples, etc.
Maybe you are interested in similar errors:
- TypeError: unhashable type: ‘list’
- TypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘str’
- TypeError: ‘<‘ not supported between instances of ‘NoneType’ and ‘int’
- TypeError: ‘AxesSubplot’ object is not subscriptable

Hello. My name is Khanh Hai Ngo. I graduated in Information Technology at VinUni. My advanced programming languages include C, C++, Python, Java, JavaScript, TypeScript, and R, which I would like to share with you. You will benefit from my content.
Name of the university: VinUni
Major: EE
Programming Languages: C, C++, Python, Java, JavaScript, TypeScript, R