Fix TypeError: unhashable type: ‘dict’ in Python [Easily]

Sharing is Caring

TypeError: unhashable type: ‘dict’ can be frustrating, especially if you’re not sure what it means or how to fix it. However, it’s an important error to understand because it can occur when you’re trying to use a dictionary as a key in another dictionary, a set, or as an element in a list.

TypeError: unhashable type: ‘dict’

In this blog post, we’ll explore what the TypeError: unhashable type: ‘dict’ error message means, why dictionaries are unhashable, and how to fix the error. We’ll also provide practical examples of how to apply the solutions to your own code.

Understanding The Error

Before we dive into the solutions, let’s first understand what the TypeError: unhashable type: ‘dict’ error message means.

A hashable object is one that has a hash value that remains constant throughout its lifetime. In Python, hashable objects are immutable objects such as strings, tuples, and numbers. Hashing is a process that takes an input (the object) and produces a fixed-size, unique output (the hash value) that identifies the object. The hash function is used to convert the object into an integer, which can be used as a key in a dictionary or as an element in a set.

However, dictionaries are unhashable because they are mutable. When a dictionary is modified, its hash value changes, making it impossible to use as a key in a dictionary or as an element in a set. This is why you get the error message when you try to use a dictionary as a key in a dictionary, as an element in a set, or as an element in a list.

To understand this better, let’s take a look at an example. Suppose we have a dictionary called my_dict:

my_dict = {'a': 1, 'b': 2, 'c': 3}

If we try to use this dictionary as a key in another dictionary, we’ll get the error message:

other_dict = {my_dict: 'some value'}
TypeError: unhashable type: 'dict'

In this example, we’re trying to use my_dict as a key in other_dict, which is not allowed because my_dict is mutable and its hash value can change.

Now that we understand why dictionaries are unhashable, let’s move on to the solutions to easily fix this error.

Also Read: Fix NameError: name ‘self’ is not defined [Easily] in Python

How To Fix TypeError: unhashable type: ‘dict’ in Python

There are two main solutions to fix the TypeError: unhashable type: ‘dict’ error: converting the dictionary to a hashable type or using a different data structure altogether.

Converting the Dictionary to a Hashable Type

One way to fix is to convert the dictionary to a hashable type. This can be done in a couple of ways:

  1. Using frozenset to convert a dictionary to a hashable type: We can use the frozenset() function to convert the dictionary to a hashable type. This function takes an iterable as an argument and returns a new frozenset object that is immutable and therefore hashable.
cssCopy codemy_dict = {'a': 1, 'b': 2, 'c': 3}
hashable_dict = frozenset(my_dict.items())

In this example, we convert the dictionary to a frozenset object using the items() method, which returns a list of key-value pairs as tuples. The frozenset() function then returns a new immutable object that can be used as a key in another dictionary or as an element in a set.

  1. Converting the dictionary to a tuple: Another way to convert a dictionary to a hashable type is to convert it to a tuple. A tuple is an immutable object that can be hashed.
cssCopy codemy_dict = {'a': 1, 'b': 2, 'c': 3}
hashable_dict = tuple(my_dict.items())

In this example, we convert the dictionary to a tuple using the items() method, which returns a list of key-value pairs as tuples. The tuple() function then returns a new immutable object that can be used as a key in another dictionary or as an element in a set.

Using a Different Data Structure:

Another way to fix is to use a different data structure altogether. Here are two options:

  1. Using a List of Dictionaries: Instead of using a dictionary as an element in a list, we can use a list of dictionaries. Since lists are mutable, we can modify them without worrying about changing their hash value.
cssCopy codemy_list = [{'a': 1, 'b': 2, 'c': 3}, {'d': 4, 'e': 5, 'f': 6}]

In this example, we have a list of dictionaries. We can access each dictionary using its index in the list, and modify the dictionaries as needed.

  1. Using a Tuple of Dictionaries: Similarly, we can use a tuple of dictionaries as an alternative data structure. Tuples are immutable and therefore hashable.
cssCopy codemy_tuple = ({'a': 1, 'b': 2, 'c': 3}, {'d': 4, 'e': 5, 'f': 6})

In this example, we have a tuple of dictionaries. We can access each dictionary using its index in the tuple, but we cannot modify the dictionaries since they are immutable.

Practical Examples

Now let’s take a look at some practical examples of how to apply these solutions to your own code.

Example 1: Converting a Dictionary to a Frozenset

cssCopy codemy_dict = {'a': 1, 'b': 2, 'c': 3}
hashable_dict = frozenset(my_dict.items())
other_dict = {hashable_dict: 'some value'}

In this example, we convert the dictionary to a frozenset object using the items() method, which returns a list of key-value pairs as tuples.

. The frozenset() function then returns a new immutable object that can be used as a key in another dictionary. We create a new dictionary, other_dict, and use the hashable_dict as a key with the value ‘some value’.

Example 2: Using a List of Dictionaries

cssCopy codemy_list = [{'a': 1, 'b': 2, 'c': 3}, {'d': 4, 'e': 5, 'f': 6}]
for item in my_list:
    print(item['a'])

In this example, we have a list of dictionaries. We iterate over the list and print the value of the key ‘a’ for each dictionary in the list. Since we are using a list of dictionaries, we can modify the dictionaries as needed.

Example 3: Using a Tuple of Dictionaries

cssCopy codemy_tuple = ({'a': 1, 'b': 2, 'c': 3}, {'d': 4, 'e': 5, 'f': 6})
for item in my_tuple:
    print(item['a'])

In this example, we have a tuple of dictionaries. We iterate over the tuple and print the value of the key ‘a’ for each dictionary in the tuple. Since we are using a tuple of dictionaries, we cannot modify the dictionaries since they are immutable.

Conclusion

The TypeError: unhashable type: ‘dict’ error occurs when you try to use a dictionary as a key in another dictionary or as an element in a set. The main solutions to fix this error are to convert the dictionary to a hashable type using frozenset or tuple, or to use a different data structure altogether, such as a list or tuple of dictionaries. By applying these solutions to your code, you can avoid the error continue working with dictionaries in Python.

FAQs

Can I modify a dictionary that has been converted to a frozenset or tuple to make it hashable?

No, once a dictionary has been converted to a frozenset or tuple, it becomes immutable and cannot be modified. If you need to modify the dictionary, you will need to create a new dictionary and convert it to a hashable type again.

Are there any downsides to converting a dictionary to a frozenset or tuple?

Yes, there are some downsides to converting a dictionary to a frozenset or tuple. Since the resulting object is immutable, you cannot modify the dictionary after it has been converted. Additionally, converting a dictionary to a hashable type can be time-consuming and may not be efficient for large dictionaries. Finally, the resulting object may not be as easy to work with as a regular dictionary.

Leave a Comment