How To Resolve “TypeError: Unhashable Type: ‘Set'” In Python

TypeError: unhashable type: 'set' in Python

To fix the TypeError: unhashable type: ‘set’ in Python, there are some solutions we have effectively tested. Follow this article to understand better.

What causes the TypeError: unhashable type: ‘set’ in Python?

Set in Python is a data structure related to set math, also known as set theory. A set can contain many elements, and these elements have no order. The elements stand indiscriminately in the Set. The elements of a set are immutable data types such as int, list, or tuple.

To declare a set: setObj = { element 1, element 2,......}

TypeError: Python throws this error when you perform an unsupported operation on an object with an invalid data type.

In short, the error TypeError: unhashable type: ‘set’ happens when you use a set as an element in another set.

Example: 

naturalSubjects = {'Math', 'Physics','Chemistry'}
socialSubjects = {'Literature', 'History','Geography','English'}
combineTwoSets = {'Math', 'Physics', 'Chemistry', socialSubjects}

print('CombineTwoSets', CombineTwoSets)

Output:

Traceback (most recent call last):
  File "code.py", line 3, in <module>
    combineTwoSets = { 'Math', 'Physics', 'Chemistry', socialSubjects }
TypeError: unhashable type: 'set'

The error in the above example is that you get the collection ‘socialSubjects’ as an element in the collection ‘CombineTwoSets’.

How to solve this error?

Use the prozenset() function

The frozenset() function provided by Python returns an immutable set. You can use it to fix this error.

Example:

  • Declare two sets.
  • Use the frozenset() function to freeze the set ‘socialSubjects’ elements.
  • The elements of the set ‘socialSubjects’ have become elements of the set ‘combineTwoSets’, so no error occurs.
naturalSubjects = {'Math', 'Physics', 'Chemistry'}
socialSubjects = {'Literature', 'History', 'Geography', 'English'}

# Use the frozenset() function
combineTwoSets = {
  'Math',
  'Physics',
  'Chemistry',
  frozenset({'Literature', 'History','Geography','English'})
}

print('Combine two sets: ' , combineTwoSets)

Output:

Combine two sets: {'Math', 'Chemistry', frozenset({'Literature', 'Geography', 'History', 'English'}), 'Physics'}

Use the Union method

Example:

  • Declare two sets.
  • Use the Union method to combine two sets.
naturalSubjects = {'Math', 'Physics','Chemistry',}
socialSubjects = {'Literature', 'History', 'Geography', 'English'}

# Use the Union to combine two sets
combineTwoSets = naturalSubjects.union(socialSubjects)

print('Combine two sets: ', combineTwoSets)

Output:

Combine two sets: {'Literature', 'English', 'Physics', 'History', 'Geography', 'Math', 'Chemistry'}

If an inner element appears in both sets, then when using the union, the element appears only once.

Example:

  • I added a ‘History’ element to ‘naturalSubjects’.
  • However, the element ‘History’ will appear only once when outputting the union of two sets.
naturalSubjects = {'Math', 'Physics', 'Chemistry', 'History'}
socialSubjects = {'Literature', 'History', 'Geography', 'English'}

# Use the Union
combineTwoSets= naturalSubjects.union(socialSubjects)

print('Combine two sets: ', combineTwoSets)

Output:

Combine two sets: {'Geography', 'Math', 'Literature', 'History', 'Chemistry', 'English', 'Physics'}

Use the add method

You can use the add method to add an element to a given set.

Example:

  • I declare a set ‘naturalSubjects’.
  • Use the add method to add an element to the set ‘naturalSubjects’.
naturalSubjects = {'Math', 'Physics','Chemistry'}

# Use the add method to add an element
naturalSubjects.add('History')

print(naturalSubjects) 

Output:

{'Chemistry', 'Physics', 'Math', 'History'}

Note: The result may differ because the Set does not arrange the elements in any order.

Summary

The above article has provided you with some solutions for the TypeError: unhashable type: ‘set’ in Python and an overview of Python’s required set of data structures. Hope you can fix this error. Thanks for reading!

Maybe you are interested:

Leave a Reply

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