TypeError: unhashable type: ‘set’

The python error TypeError: unhashable type: ‘set’ happens when a set is added to another set or used as a key in a dictionary. The set is an unhashable object in python. The unhashable objects are not allowed in set or dictionary key. The dictionary or set hashes the object and uses the hash value as a primary reference to the key.

The python Set is a mutable object. The Set element can be changed at any given time. The hashing is an encoding process and produces a unique key that is used in the search of data. Hashing is an algorithm that allows you to store an object that can be retrieved quickly if required. The python objects such as list, set, dictionary, byte array and custom classes are objects that can not be hashed

The set must be cast to an immutable object before it is added to another set or as a dictionary key. The python tuple or frozen set is used to store the set value in another set or as a dictionary key.



Exception

The error TypeError: unhashable type: ‘set’ will be shown as below the sack trace.

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 3, in <module>
    c = {a:1, b:2}
TypeError: unhashable type: 'set'
[Finished in 0.1s with exit code 1]


How to reproduce this issue

The python Set is an non-hashable object. Create a set object in python. Add the set object to another set object. Or, add a set object as one of the key in another dictionary. Since the unhashable object is not allowed in set or dictionary, the error will be thrown

test.py

a = {1 , 2}
b = {3 , 4}
c = {a:1, b:2}
print c[a]

Output

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 3, in <module>
    c = {a:1, b:2}
TypeError: unhashable type: 'set'
[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 with hash code. The dictionary maintains the unique keys to store values. The dictionary is an unhashable object that can be modified at any given time. The error TypeError: unhashable type: ‘set’ is due to the addition of an unhashable object to the set or dictionary key.



Solution 1

The python Set can not be added as a key of a dictionary. The set is not an immutable object. The set is to be casted to tuple before storing in to a dictionary. The tuple is created using the set elements as shown in the example below. The immutable objects can be stored in the dictionary. This will resolve the error TypeError: unhashable type: ‘set’

test.py

a = tuple({1 , 2})
b = tuple({3 , 4})
c = {a:1, b:2}
print c[a]

Output

1
[Finished in 0.0s]


Solution 2

The python set is a mutable object. The python frozen set is a immutable object that works same as set. The python set can be casted into a frozen set before inserting it into another set or as a key in a dictionary. The frozen set is a set object that is immutable.

test.py

a = frozenset({1 , 2})
b = frozenset({3 , 4})
c = {a:1, b:2}
print c[a]


Solution 3

The another option to add the Set in a dictionary is to store as value. The example below illustrates how to add a set into a dictionary as a value. In a dictionary the mutable objects can be stored as a value

test.py

a = {1 , 2}
b = {3 , 4}
c = {1:a, 2:b}
print c[1]

Output

set([1, 2])
[Finished in 0.1s]


Solution 4

The set can not be added in another Set object. The set is not an immutable object. The set is to be converted to tuple or frozenset before storing in Python Set. The immutable objects can be stored in Set. This will resolve the error TypeError: unhashable type: ‘set’

test.py

a = {1 , 2}
b = {3 , 4}
c = {a, b}
print c[a]

Exception

Traceback (most recent call last):
  File "/Users/knatarajan2/Desktop/test.py", line 3, in <module>
    c = {a, b}
TypeError: unhashable type: 'set'
[Finished in 0.1s with exit code 1]

Solution

a = frozenset({1 , 2})
b = tuple({3 , 4})
c = {a, b}
print c

Output

set([frozenset([1, 2]), (3, 4)])
[Finished in 0.0s]



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *