The TypeError: unhashable type: ‘dict’ error happens when you try to use a dict as a hash argument when adding as a key in another dict or in a set. The dict is an unhashable object that you’ve been trying to hash. The python unhashable type dict is not allowed in set or dict key. The dictionary or set hashes the object and uses the hash value as a primary reference to the key. Else, the error TypeError: unhashable type: ‘dict’ will be thrown in python.
The dictionary is a mutable object. The dictionary element can be modified at any given time. The hashing is a data encoding process and generates unique key that uses in data search. Hashing is an algorithm that lets you store object that can be retrieved quickly if needed. The python objects such as list, set, dict, byte array and custom classes are objects which can not be hashed
This problem can be solved by casting dictionary to tuple. As the tuple is immutable object, it can be used either in set or as key in dictionary.
Exception
The error TypeError: unhashable type: ‘dict’ will be shown as below the sack trace.
Traceback (most recent call last):
File "/Users/python/Desktop/test.py", line 2, in <module>
b = {a:1, 2:3}
TypeError: unhashable type: 'dict'
[Finished in 0.1s with exit code 1]
How to reproduce this error
The dictionary is an unhashable object. Create a dictionary object in python. Add the dictionary object in a set object. Or, add the dictionary object as one of the key in another dictionary. Since the unhashable object is not permitted in set or dictionary, the error will be thrown
test.py
a = {1:1, 2:2 , 3:3}
b = {a:1, 2:3}
print b
Output
Traceback (most recent call last):
File "/Users/python/Desktop/test.py", line 2, in <module>
b = {a:1, 2:3}
TypeError: unhashable type: 'dict'
[Finished in 0.1s with exit code 1]
Root Cause
The set or dictionary uses hashing algorithm to store and retrieve the elements in the data structure. The set maintains the unique objects using hash. The dictionary maintains the unique keys to store values. The dictionary is an unhashable object which can be changed at any given time. The error TypeError: unhashable type: ‘dict’ is due to adding an unhashable object in set or dictionary key.
Solution 1
The dictionary can not be added as a key of another dictionary. The dictionary is not an immutable object. The dictionary is to be converted to tuple before storing in to another dictionary. The tuple is created using the keys of the dictionary as shown in the example below. The immutable objects can be stored in the dictionary. This will resolve the error TypeError: unhashable type: ‘dict’
test.py
a = tuple({1:1, 2:2 , 3:3})
b = {a:1, 2:3}
print b
Output
{2: 3, (1, 2, 3): 1}
[Finished in 0.1s]
Solution 2
The another option to add the dictionary in another dictionary is to store as value. The example below illustrates how to add a dictionary into another dictionary as a value. In a dictionary the mutable objects can be stored as a value
test.py
a = {1:1, 2:2 , 3:3}
b = {1:a, 2:3}
print b
Output
{1: {1: 1, 2: 2, 3: 3}, 2: 3}
[Finished in 0.1s]
Solution 3
The dictionary can not be added in Set object. The dictionary is not an immutable object. The dictionary is to be converted to tuple before storing in Python Set. The immutable objects can be stored in Set. This will resolve the error TypeError: unhashable type: ‘dict’
test.py
a = {1:1, 2:2 , 3:3}
b = {a, 2}
print b
Exception
Traceback (most recent call last):
File "/Users/python/Desktop/test.py", line 2, in <module>
b = {a, 2}
TypeError: unhashable type: 'dict'
[Finished in 0.1s with exit code 1]
Solution
a = tuple({1:1, 2:2 , 3:3})
b = {a, 2}
print b
Output
{2, (1, 2, 3)}
[Finished in 0.1s]