Warning: session_start(): open(/tmp/sess_a495a6008ff5e1393c09469d7e51dde3, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
“TypeError: Unhashable Type: 'Dict'” Error In Python - How To Fix? - LearnShareIT

“TypeError: Unhashable Type: ‘Dict’” Error In Python – How To Fix?

TypeError: unhashable type: ‘dict’

This tutorial will guide you step by step on how to fix the “TypeError: unhashable type: ‘dict’” error. The problem often encountered when we use unhashable objects as keys in our dictionaries. 

What causes the “TypeError: unhashable type: ‘dict’” error?

Reproduce the “TypeError: unhashable type: ‘dict’” error in Python

Example 1: 

In this example, I will create a dictionary that contains the numbers of different types of animals in a zoo and try to find out which animal has a quantity of more than four.

animals = [
    {"name": "zebra", "quantity": 7},
    {"name": "tiger", "quantity": 4},
    {"name": "cheetah", "quantity": 1},
    {"name": "elephant", "quantity": 2},
    {"name": "snake", "quantity": 10}
]

moreThanFour = {}

for i in animals:
    if i["quantity"] > 4:
        moreThanFour[i] = i["quantity"]

print(moreThanFour)

Output

Traceback (most recent call last):
  File "<string>", line 13, in <module>
    moreThanFour[i] = i["quantity"]
TypeError: unhashable type: 'dict'

And we have run into this error. Here is the reason:

If we take a look at line 13: 

moreThanFour[i] = i["quantity"]

Our program will run this code in the if statement: 

moreThanFour[{"name": "zebra", "quantity": 7}] = 7

This means that we are trying to use a dictionary as a key in a dictionary.

The dictionary in Python has the keys and the values. The keys have to be hashable. In Python, only immutable objects: integer, string, float, boolean, unicode, and tuple are hashable. Dictionary is not a hashable object, so it could not be used as a key in a dictionary.

How to solve the error

Solution 1: Use a valid key for the dictionary

For example 1, we can use i[“name”] as the key for our dictionary.  

Fix code for example 1: 

animals = [
    {"name": "zebra", "quantity": 7},
    {"name": "tiger", "quantity": 4},
    {"name": "cheetah", "quantity": 1},
    {"name": "elephant", "quantity": 2},
    {"name": "snake", "quantity": 10}
]

moreThanFour = {}

for i in animals:
    if i["quantity"] > 4:
        moreThanFour[i["name"]] = i["quantity"]

print(moreThanFour)

Output

{'zebra': 7, 'snake': 10}

Solution 2: Convert the dictionary into a hashable object

Let’s look at another example that also runs into the same error.

Example 2: 

animals = {
    "tall": "giraffe",
    "big": "hippo"
}

dictionary = {
    animals: "elephant"
}

print(dictionary)

Output

Traceback (most recent call last):
  File "<string>", line 6, in <module>
    dictionary = {
TypeError: unhashable type: 'dict'

Again, animals is a dictionary. And since the dictionary is not a hashable object, when we use animals as a key, we will run into the same error.

We can fix it by converting the dictionary into a hash object like a tuple or frozenset. Using:

or

  • iterable: an iterable.

Fixed code for example 2:

animals = {
    "tall": "giraffe",
    "big": "hippo",
}

dictionary = {
    tuple(animals): "elephant"
}

print(dictionary)

Output

{('tall', 'big'): 'elephant'}

Summary

“TypeError: unhashable type: ‘dict’” is a common error in Python. It often occurs when we try to use an unhashable object as a key in our dictionary. Therefore, to avoid this problem, make sure that our keys are immutable objects or, if not, convert them into hashable objects like strings, integers, tuples, etc.

Maybe you are interested in similar errors:

Leave a Reply

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