TypeError: unhashable type: ‘dict’

In Python, all the dictionary keys must be hashable, so if you use any unhashable key type while adding a key into the dictionary, you will encounter TypeError: unhashable type: ‘dict’

TypeError: unhashable type: ‘dict’

Dictionary in Python is an unordered collection to store data values in key:value pairs. Key acts as an identifier to access and retrieve the value in the dictionary.

The keys can contain only immutable hashable types such as strings, boolean, integers, tuples are hashable, which means the value doesn’t change during its lifetime. It will allow Python to create unique hash values for the keys.

If you try to add any unhashable object such as a dictionary to the key, you will encounter  TypeError: unhashable type: ‘dict’. Let’s take a simple example to demonstrate this issue.

fruits={"Apple":100,"Orange":50,"Grapes":40}
print("The fruits dictionary is ", fruits)
stock_quantity={fruits:20}
print("The stock quantity of fruits", stock_quantity)

Output

The fruits dictionary is  {'Apple': 100, 'Orange': 50, 'Grapes': 40}
Traceback (most recent call last):
  File "c:\Projects\Tryouts\Python Tutorial.py", line 3, in <module>
    stock_quantity={fruits:20}
TypeError: unhashable type: 'dict

In the above code, we create a dictionary of fruits with name as a key and price as a value. When we print the dictionary, it will print without any issue because the key is of string which is hashable.

Next, we created another dictionary, stock_quantity, and tried assigning a fruits dictionary as a key that cannot be hashed. Dict is not hashable in Python as it’s mutable, so if you dict, list, set as a key, you will get TypeError: unhashable type: ‘dict’.

Solution to TypeError: unhashable type: ‘dict’.

There are multiple solutions to resolve the unhashable type: ‘dict’ error. Let’s take a look at each of these solutions in detail.

By Converting into a tuple

Since a dictionary cannot be added as a key to another dictionary and its mutable object, we need to convert this into a tuple before storing it into another dictionary.

fruits=tuple({"Apple":100,"Orange":50,"Grapes":40})
print("The fruits tuple is ", fruits)
stock_quantity={fruits:20}
print("The stock quantity of fruits", stock_quantity)

Output

The fruits tuple is  ('Apple', 'Orange', 'Grapes')
The stock quantity of fruits {('Apple', 'Orange', 'Grapes'): 20}

By Adding Dictionary as a value in another dictionary

Instead of adding it as a key, you could add it as a value in another dictionary if it makes sense. Below is one such example where we are adding the dictionary as a value in another dictionary.

fruits={"Apple":100,"Orange":50,"Grapes":40}
print("The fruits dictionary is ", fruits)
price={"fruits_price":fruits}
print("The price for each fruits are ", price)

Output

The fruits dictionary is  {'Apple': 100, 'Orange': 50, 'Grapes': 40}
The price for each fruits are  {'fruits_price': {'Apple': 100, 'Orange': 50, 'Grapes': 40}}
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like
Python String Isidentifier()

Python String isidentifier()

Table of Contents Hide isidentifier() Syntaxisidentifier() Parametersisidentifier() Return ValueExample 1: How isidentifier() works? Example 1: How to use isidentifier() method? The Python String isidentifier() method is a built-in function that…
View Post
List Of Python Free Ebooks

List of Free Python Books

Table of Contents Hide Python SuccinctlyPython Data Science HandbookThink Python 2nd EditionAutomate the Boring Stuff with PythonMaking Games with Python & PygameData Structures and Algorithms in Python Google Python Style GuideA…
View Post