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

Get notified of the best deals on our WordPress themes.

You May Also Like
Python Bytearray()

Python bytearray()

Table of Contents Hide bytearray() Syntax bytearray() Parametersbytearray() Return ValueExample 1: Array of bytes of given integer sizeExample 2: Array of bytes from a stringExample 3: Array of bytes from an…
View Post
How To Rename Columns In Pandas Dataframe

How to rename columns in Pandas DataFrame

Table of Contents Hide Method 1: Rename Specific column names in Pandas DataFrameMethod 2: Rename all column names in Pandas DataFrameMethod 3: Replace specific characters in Columns of Pandas DataFrame…
View Post
Python Dir()

Python dir()

Table of Contents Hide dir() Syntax dir() Parametersdir() Return ValueExample 1: How dir() works?Example 2: When no parameters are passed to dir() method with and without importing external libraries.Example 3: When a module…
View Post