Fix TypeError: unhashable type: 'dict' in Python

Fix TypeError: unhashable type: 'dict' in Python

A TypeError occurs in Python when an operation or function is applied to an object of inappropriate type. One such type error is "TypeError: unhashable type: dict," which is raised when we try to use a dictionary as a key in a set or use it as a key in another dictionary.

In Python, hashable objects are used as keys in dictionaries and elements in sets. A hashable object has a hash value that never changes during its lifetime (it needs a hash() method), and it can be compared to other objects for equality (it needs an eq() method). To reproduce this error, you can use the following examples:

Example 1: Using a dictionary as a key in a set

dict1 = {'Name': 'John Doe', 'Age': 30}
set1 = {dict1}

Output:

TypeError: unhashable type: 'dict'

In the above example, we are trying to add the dictionary dict1 to a set set1. Since dictionaries are mutable, their hash value can change, and thus they cannot be used as keys in sets.

Example 2: Using a dictionary as a key in another dictionary

dict1 = {'Name': 'John Doe', 'Age': 30}
dict2 = {dict1: 'Details'}

Output:

TypeError: unhashable type: 'dict'

In the above example, we are trying to use dict1 as a key in another dictionary dict2. Since dictionaries are mutable, their hash value can change, and thus they cannot be used as keys in another dictionary.

Solution for TypeError: unhashable type: 'dict'

One solution to the "TypeError: unhashable type: dict" is to convert the dictionary to a tuple or a frozenset. Tuples and frozensets are hashable and can be used as keys in sets and dictionaries.

Example:

dict1 = {'Name': 'John Doe', 'Age': 30}
tuple1 = tuple(dict1.items())
set1 = {tuple1}
print(set1)

Output:

{(('Name', 'John Doe'), ('Age', 30))}

In the above example, we have converted the dictionary dict1 to a tuple tuple1 using the tuple() method and the .items() method of the dictionary. Now, we can use tuple1 as a key in the set set1.

Conclusion

In conclusion, the "TypeError: unhashable type: dict" error occurs when we try to use a dictionary as a key in a set or another dictionary. To resolve this error, we need to convert the dictionary to a tuple or frozenset, which are hashable and can be used as keys.

See also: TypeError: 'module' object is not callable in Python

TypeError: unhashable type: 'dict' - FAQs

What are the implications of using unhashable types as keys in dictionaries or elements in sets?

When using unhashable types as keys in dictionaries or elements in sets, the hash value of the object can change during its lifetime, causing the key-value pair or the element to become inaccessible or disappear. This can result in unexpected behavior and make the code difficult to debug.

Can I use lists as keys in dictionaries or elements in sets?

No, you cannot use lists as keys in dictionaries or elements in sets because lists are mutable and their hash value can change. Just like dictionaries, lists are unhashable types and cannot be used as keys or elements in sets.

Is it possible to use custom objects as keys in dictionaries or elements in sets?

Yes, it is possible to use custom objects as keys in dictionaries or elements in sets, but only if they are hashable. To make a custom object hashable, it must define both the __hash__() and __eq__() methods. These methods should ensure that two instances of the same object have the same hash value and can be compared for equality.