We walk through how you would solve this error so that you are able to get back on track with writing code without any errors or warnings!
Table of Contents
TypeError: unhashable type: ‘dict’
Dictionaries are one of the most powerful tools in Python. They consist of two parts: keys and values. Keys are identifiers that bind to a value, which can be retrieved when referencing the key; for example, “name” is bound to John Doe’s name.
Dictionaries cannot be used as a dictionary key because they are not hashable objects like strings or integers – only immutable objects such as tuples and frozensets are hashable with some exceptions.
To add an item to a dictionary, you must specify a valid hashable key (for instance “name”) instead of using {“name”: “test”}.
Let’s have a look at an example.
Example
Here, we create a software that find the drinks left more than three times from one dictionary to another.
Begin by creating a drinks list that includes dictionaries for each drink. We also create a lexicon in which we can keep track of drinks that are left more than three.
Three dictionaries are included in our “drinks” list. There are two keys and values in each dictionary. The words “drink” and “left” are important.
Now we’ll construct a for loop that iterates over our list of drinks, looking for those that are left more than three. Those drinks will be added to the dictionary “left more than three”:
Code
drinks = [
{
"name": "Soda", "left": 3
},
{
"name": "Beer", "left": 7
},
{
"name": "Wine", "left": 9
}
]
left_more_than_three = {}
for d in drinks:
if d["left"] > 3:
left_more_than_three[d] = d["left"]
print("More than 3 " + d["name"] + "are left.")
print(left_more_than_three)
We check whether the value of “left” in each dictionary is bigger than 3 in our for loop. If that’s the case, we’ll add it to our “left more than three” dictionary. The user is then informed that the drink is left more than three via a message printed to the console.
We print the “left more than three” dictionary to the console once our loop has completed.
To ensure that our software works, run the following code:
Output: Error Occurred

Our code throws an exception.
Solution
Because we’re attempting to generate a dictionary key with another dictionary, our code fails.
The value of “d” is a dictionary from our list of “drinks.” This means that if we try to add something to the “left more than three” dictionary, we are inadvertently adding a dictionary as a key:
Our code tries to run: This is invalid because we’re trying to add a dictionary as a key in a dictionary when we run our “if” statement on the “Soda” cake. Using d[“name”] as the name of our dictionary key, we can solve this problem:
drinks = [
{
"name": "Soda", "left": 3
},
{
"name": "Beer", "left": 7
},
{
"name": "Wine", "left": 9
}
]
left_more_than_three = {}
for d in drinks:
if d["left"] > 3:
left_more_than_three[d["name"]] = d["left"]
print("More than 3 " + d["name"] + "are left.")
print(left_more_than_three)
Use this altered code to run our code:
Output

Our code works perfectly. Rather than utilizing a dictionary, we now use the name of each drink as a key.
Conclusion
The “TypeError: unhashable type: ‘dict’” error is raised when you try to create an item in a dictionary whose key is an unhashable object. Only immutable objects like strings, tuples, and integers can be used as a key in a dictionary.
This means that if you want to use something else as the key for your item, then you need to change it into one of those things first.
Now that we know what causes this error and how to fix it, let’s take some time to explore some more mistakes that Python programmers make!