TypeError: unhashable type: ‘dict’ (Python)

In Python, dictionaries are used to store multiple unordered collections of data as pairs of key values. A Python dictionary has a unique “key” that is used to store the value. The values can be accessed in the program using the respective keys. A “key” in a Python dictionary must be a hashable object. The hashable object means that the object has some value of hash and also the object is immutable. If the “key” in Python is not hashable, then the error “TypeError: unhashable type: dict” occurs in the program.

This post will explain the causes and solutions of “TypeError: unhashable type: dict” in Python.

Reason: Using Dictionary as a Key

The main reason which causes the error “unhashable type dict” is when a user tries to use a dictionary as the key of another dictionary in Python.

The above snippet shows the “TypeError” because the “dict_1” dictionary variable is passed as a key to another dictionary, “dict_val”.

Solution 1: Convert Dictionary to Tuple or JSON String

To resolve this error, the inbuilt “tuple()” and “json.dumps()” function is used in Python. The dictionary is not hashable, so to convert it into hashable, the “tuple()” and “json.dumps()” function is used in the below code. Both these functions are applicable, and you can use any one of these.

Code:

import json
#Using json.dumps() Function
dict_1 = json.dumps({'student':'John','age': 45})
dict_val = {dict_1: 'Lily', 'Marks': 92}
print(dict_val)

#Using tuple() Function
dict_1 = tuple({'student':'John','age': 45})
dict_val = {dict_1: 'Lily', 'Marks': 92}
print(dict_val)

In the above code:

  • The input dictionary “dict_1” is used as a key for another dictionary “dict_val”.
  • The “json” module is imported at the start of the program to access the “json.dumps()” function.
  • The “json.dumps()” function is used to convert the dictionary into hashable json strings.
  • Another function, “tuple(),” is used in the above code to resolve this error by converting the dictionary into a tuple.
  • The converted dictionary value is used as a dictionary key without any error.

Output:

The above output shows that the use of the “tuple()” and “json.dumps()” functions resolve the stated problem.

Solution 2: Using Dictionary as a Value

To fix the “TypeError: unhashable type: dict” in Python, the dictionary is also used as a value for the key. In the below code, the input dictionary is used as the value for another dictionary.

Code:

dict_1 = {'student':'John','age': 45}
dict_val = {'Info': dict_1, 'Marks': 92}
print(dict_val)

In the above code:

  • The “dict_1” dictionary variable is initialized in the program.
  • The “dict_1” variable is used as a value for the dictionary “dict_val”.

Output:

The above output verified that using a dictionary as a value for another dictionary resolves the stated error.

How to Check Hashable Objects in Python?

To check the hashable objects, the “hash()” function is used in Python. In the following coding example, the “hash()” function is used to check whether the function is hashable or not:

Code:

val = hash({'Name': 'Alex'})
print(val)

In the above code, the dictionary is passed inside the parentheses of the “hash()” function to check whether it is hashable or not.

Output:

The above output shows an error because the “dictionary” object is not hashable in Python.

Conclusion

The “TypeError: unhashable type: dict” error occurs when a user tries to use the dictionary as a key for another dictionary in Python. To resolve this error, various solutions are provided, such as converting the dictionary to a tuple or JSON string, using the dictionary as a value, etc. You can also check whether the object is hashable in Python using the “hash()” function. This Python post has explained the reason and solutions of “TypeError: unhashable type dict” using appropriate examples.