ItsMyCode |
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}}
The post TypeError: unhashable type: ‘dict’ appeared first on ItsMyCode.
Top comments (0)