The Python language is specific about what can be used as a key in a dictionary. In a Python dictionary, all keys must be hashable.
If you try to use an unhashable key type when adding a key to a dictionary, you’ll encounter the “TypeError: unhashable type: ‘dict’” error.
In this guide, we talk about what this error means and why it is raised. We walk through an example of this error so you can learn how to solve it in your code.
TypeError: unhashable type: ‘dict’
Dictionaries consist of two parts: keys and values. Keys are the identifiers that are bound to a value. When you reference a key, you’ll be able to retrieve the value associated with that key.
Only hashable objects can be keys in a dictionary. Immutable objects such as strings, integers, tuples, and frozensets are hashable, with some exceptions. Dictionaries, therefore, cannot be used as a key in a dictionary.
To add an item to a dictionary, you must specify a valid hashable key. For instance, “name” is a valid key, but { “name”: “test” } is not.
An Example Scenario
Here, we write a program that adds all the cakes that have been sold more than five times at a bakery from one dictionary to another dictionary.
Start by declaring a list of cakes which contains dictionaries about each cake. We also define a dictionary in which we can store the cakes that have been sold more than five times.
cakes = [ { "name": "Black Forest Gateau", "sold": 3 }, { "name": "Carrot Cake", "sold": 7 }, { "name": "Coconut and Lime Cake", "sold": 9 } ] sold_more_than_five = {}
Our “cakes” list contains three dictionaries. Each dictionary contains two keys and values. The key names are “cake” and “sold”.
Now, we write a for loop that goes through our list of cakes and finds the ones that have been sold more than five times. Those cakes will be added to the “sold_more_than_five” dictionary:
for c in cakes: if c["sold"] > 5: sold_more_than_five[c] = c["sold"] print(c["name"] + " has been sold more than five times.") print(sold_more_than_five)
In our for loop, we compare whether the value of “sold” in each dictionary is greater than 5. If it is, that item is added to our “sold_more_than_five” dictionary. Then, a message is printed to the console informing the user that the particular cake has been sold more than five times.
Once our loop has run, we print the “sold_more_than_five” dictionary to the console.
Run our code to make sure our program works:
Traceback (most recent call last): File "main.py", line 16, in <module> sold_more_than_five[c] = c["sold"] TypeError: unhashable type: 'dict'
Our code returns an error.
The Solution
Our code does not work because we are trying to create a dictionary key using another dictionary.
The value of “c” is equal to a dictionary from our “cakes” list. This means when we try to add an item to the “sold_more_than_five” dictionary, we are accidentally trying to add a dictionary as a key:
sold_more_than_five[c] = c["sold"]
When our “if” statement is run on the “Carrot Cake” cake, our code tries to run:
sold_more_than_five[{"name": "Carrot Cake", "sold": 7}] = 7
This is invalid because we’re trying to add a dictionary as a key in a dictionary. We can solve this problem by using c[“name”] as the name of our dictionary key:
sold_more_than_five[c["name"]] = c["sold"]
Run our code with this revised code:
Carrot Cake has been sold more than five times. Coconut and Lime Cake has been sold more than five times. {'Carrot Cake': 7, 'Coconut and Lime Cake': 9}
Our code runs successfully. We’re now using the name of each cake as a key rather than a dictionary.
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.
To solve this error, make sure that you only use hashable objects when creating an item in a dictionary. Now you’re ready to solve this common Python error like a professional developer!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.