Typeerror: unhashable type: ‘set’ [SOLVED]

One of the errors we encounter in Python is the TypeError: unhashable type: ‘set’. This error occurs when we try to use a set as a key in a dictionary or an element in a set.

In this guide, we will explore the reason behind this error, what sets are, and how to avoid this error in Python.

Primarily, in Python data types play an essential role in programming, and using the wrong data type or lack of understanding of them can result in errors.

What are Sets in Python?

A set is an unordered collection of unique elements that can be of any data type.

Additionally, sets are defined by enclosing a comma-separated list of values within curly braces { } or by using the built-in set() function.

Example of sets:

sample_set = {1, 2, 3, 'Hello ITSOURCECODE! ', (4, 5, 6)}
sample_set_2 = set([1, 2, 3, 3, 3]) # creating a set using the set() function

Before we dive into the error, we need to understand the concept of hashable and unhashable types in Python.

Hashable vs. Unhashable Types


A hashable object is an object that has a hash value that remains the same throughout its lifetime. Basically, a hash value is an integer that is used to compare and identify objects quickly.

Particularly, Immutable data types such as numbers, strings, and tuples are hashable.
Meanwhile, mutable data types such as lists and dictionaries are unhashable.

What is Typeerror: unhashable type: ‘set’?

The TypeError: unhashable type: ‘set’ occurs when we try to use a set as a key in a dictionary or as an element in a set.

As mentioned earlier, sets are mutable objects, and therefore, they are unhashable. That’s why when we try to use a set as a key in a dictionary or as an element in a set, we get the error.

Here’s how the error occurs:

my_dict = {{1, 2}: 'value'} 

Here is the output:

Traceback (most recent call last):
  File "C:\Users\Windows\PycharmProjects\pythonProject\main.py", line 1, in <module>
    my_dict = {{1, 2}: 'value'}
TypeError: unhashable type: 'set'

In the above example, we are trying to use a set as a key in a dictionary, and Python throws a TypeError because the set is unhashable.

How to fix Typeerror: unhashable type: ‘set’

Now, let’s look at three ways to fix this error Typeerror: unhashable type: ‘set’, along with an example code and the output of each solution.

Solution 1: Convert the set to a frozenset

A frozenset is an immutable version of a set, which means it can be used as a key in a dictionary or an element in another set.

You can convert a set to a frozenset using the built-in frozenset() function.

Example code:

sample_set = set([1, 2, 3])
sample_dict = {frozenset(sample_set ): 'hello'}
print(sample_dict )

Output:

{frozenset({1, 2, 3}): 'hello'}

In this example, we create a set called sample_set with the elements 1, 2, and 3. We then create a dictionary called sample_dict with the key as the frozenset version of my_set and the value as the string ‘hello‘.

Finally, we print sample_dict , which outputs {frozenset({1, 2, 3}): ‘hello’}.

Solution 2: Use a tuple instead of a set

Tuples are hashable in Python, which means they can be used as keys in dictionaries or elements in other sets.

We can convert a set to a tuple using the built-in tuple() function.

Example code:

sample_set = set([1, 2, 3])
sample_dict = {(tuple(sample_set )): 'hello'}
print(sample_dict )

Here is the output when we run the code:

{(1, 2, 3): 'hello'}

In this example, we create a set called sample_set with the elements 1, 2, and 3. We then create a dictionary called sample_dict with the key as the tuple version of sample_set and the value as the string ‘hello‘.

Finally, we print sample_dict , which outputs {(1, 2, 3): ‘hello’}.

Solution 3: Use a hashable set of frozensets

If we need to use sets as elements of other sets or values in dictionaries, we can use a set of frozensets instead.

This works because frozensets are hashable, and therefore can be used as elements in another set.

sample_set = set([1, 2, 3])
sample_frozenset = frozenset(sample_set)
sample_set_of_frozensets = set([sample_frozenset])
print(sample_set_of_frozensets)

Output:

{frozenset({1, 2, 3})}

In this example, we create a set called sample_set with the elements 1, 2, and 3. We then create a frozenset called sample_frozenset from sample_set.

Finally, we create a set called sample_set_of_frozensets with sample_frozenset as its only element, and print it. The output is {frozenset({1, 2, 3})}.

FAQs in Typeerror: unhashable type: ‘set’

What is the difference between a set and a frozenset?

The main difference between a set and a frozenset is that a set is mutable, meaning it can be modified, while a frozenset is immutable, meaning it cannot be modified after it is created.

Also, frozensets are hashable, while sets are not.

Conclusion

In Python, understanding the difference between hashable and unhashable types is crucial, and using an unhashable type as a key or element in a set can result in the TypeError: unhashable type: ‘set’.

To fix this error, we can convert the set into a frozenset, which is an immutable version of a set and is hashable.

We hope that this guide has helped you resolve this error and get back to coding.

If you are finding solutions to some errors you might encounter we also have Typeerror: nonetype object is not callable.

Thank you for reading!