DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 963,503 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Srinivas Ramakrishna for ItsMyCode

Posted on • Originally published at itsmycode.com on

TypeError: unhashable type: β€˜dict’

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)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

Output

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

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)
Enter fullscreen mode Exit fullscreen mode

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}}
Enter fullscreen mode Exit fullscreen mode

The post TypeError: unhashable type: β€˜dict’ appeared first on ItsMyCode.

Top comments (0)

Update Your DEV Experience Level:

Settings

Go to your customization settings to nudge your home feed to show content more relevant to your developer experience level. πŸ›