Development

Unhashable Type 'dict'

Captain Salem 2 min read
Unhashable Type 'dict'

In Python, a dictionary refers to an unordered collection of key-value pairs. Each key is mapped to a specific value within the dictionary. A dictionary's key is unique and is used to access the corresponding value.

However, a key of a Python dictionary must be hashable. A hashable object means that the object is immutable and can produce a unique hash value.

Hence, using an unhashable object as a dictionary's key will result in an error.

Let us discuss how we can resolve this error.

Example Error

Consider the code below that illustrates how this type of error occurs.

>>> developers = {"games": 200, "database": 100, "full_stack": 500}
>>> employees = {developers: 800}

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'dict'

In the example above, we start by defining a dictionary that holds the type of developer and the total number.

We create an employee dictionary that takes the developer's dictionary as the key in the second part.

Since a dictionary is unhashable, Python cannot use it as a dictionary's key.

Python Check If Object is Hashable

The question comes, how do I know if an object is hashable or not? In Python, you can use the hash function to determine if an object is hashable.

An example is as shown:

>>> hash("key")
-4481039435095198476

If an object is hashable, it should return a unique hash as shown in the example above. Since a string is hashable, it returns a unique hash.

If you attempt the same operation on a dictionary or a list, you will get an error as shown:

>>> hash([1,2,3,4,5])

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'

It is not hashable since a list is mutable, as shown in error above.

Fix: TypeError: unhashable type: 'dict'

If you want to use a dictionary as a key for another dictionary, you can do this by first converting it into a tuple.

A tuple is hashable as shown in the example below:

>>> hash((1,2,3,4,5))
-5659871693760987716

Let us solve our first error by converting the developer's dictionary into a tuple:

>>> developers = tuple(developers)
>>> print(developers)
('games', 'database', 'full_stack')

Once we convert it into a tuple, we can use it as key in the dictionary as shown:

>>> employees = {developers: 800}
>>> print(employees)
{('games', 'database', 'full_stack'): 800}

Fix 2

Another solution is using the dictionary as a value. For example, we can use the developer's dictionary as a value for the employees' dictionary.

Take the code illustration below:

>>> developers = {"games": 200, "database": 100, "full_stack": 500}
>>> employees = {"employees": developers}

>>> print(employees)
{'employees': {'games': 200, 'database': 100, 'full_stack': 500}}

In this case, the code does not return an error as we are using the dictionary as value for a new dictionary.

Using a For Loop

Another unconventional method is using a for loop. You can iterate over each key and value pairs of one dictionary and add them to a new dictionary.

Consider the example below:

>>> developers = {"games": 200, "database": 100, "full_stack": 500}
>>> for k, v in developers.items():
... employees[k] = v
...
>>> employees
{'employees': {'games': 200, 'database': 100, 'full_stack': 500}, 'games': 200, 'database': 100, 'full_stack': 500}

In this case, we use a for loop to iterate over the developers' dictionary items and append them to the employees' dict.

Conclusion

This article explored three methods of fixing the TypeError: Unhashable type: 'dict' error in Python.

Share
Comments
More from Cloudenv

Cloudenv

Developer Tips, Tricks and Tutorials.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Cloudenv.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.