A Python dictionary is a data container object that stores its elements in the form of
key:value
pair. The dictionary hashes the key data and maps it with the corresponding value. According to the Python syntax, we can only use an immutable data structure such as string, tuple, integers, and floats as a key because Python can hash these data types.
If we try to use a mutable or unhashable data object as a key, there we will encounter an unhashable error. Similarly, if we use a dictionary object as a key in another dictionary, Python will raise the error, "TypeError: unhashable type: 'dict'".
In this Python tutorial, we will discuss this error in detail and learn how to solve it. To tackle this error, we will also walk through a common example scenario.
Python Error: TypeError: unhashable type: 'dict'
As we know that a Python dictionary contains its elements in the form of
key:value
pairs. And according to the dictionary syntax, a key should be an immutable data type that can be hashed, and a value could be any data object.
But if we try to use a dictionary object which is a mutable data type as a key, there Python raises the
TypeError: unhashable type: 'dict'
Error. This is because Python hashes the dictionary's key data to its corresponding value, and the immutable data objects like the list and dictionary are unhashable.
We can break the Error statement
TypeError: unhashable type: 'dict'
into two parts.
- TypeError (Exception Type)
- unhashable type: 'dict' (Error Message)
1. TypeError
TypeError is a standard Python exception. Python raises this exception when we try to apply an invalid operation or function on an inappropriate data type. This Exception also raises when we pass a wrong data type to a function.
2. unhashable type: 'dict'
unhashable type: 'dict'
is the error message statement. This error message is only raised when we try to hash a dictionary object in Python. Python comes with an inbuilt
hash()
function, that can hash an immutable data type to a hashable integer value. Python dictionary also uses the
hash()
function to hash its keys. If the key data is a dictionary itself, the
hash()
function could not hash that key and raise this error.
Error Example 1: Try to hash a dictionary using the hash() function
requirements = {"LED":100,
"100m Wire":40,
"Switches":100,
"Motors":15,
"Fuse":250
}
# try to hash the requirements dictionary (Error)
hash(requirements)
Output
Traceback (most recent call last):
File "main.py", line 8, in
hash(requirements)
TypeError: unhashable type: 'dict'
Error Example 2: Use a dictionary as a key for another dictionary
dict1 = { 1:"a" }
# use dict1 as a key for dict2
dict2 = {dict1: "b"} #error
Output
Traceback (most recent call last):
File "main.py", line 4, in
dict2 = {dict1: "b"} #error
TypeError: unhashable type: 'dict'
In the above examples, we are getting the same error. In example 1, we are getting the error because we are trying to hash the dictionary object using
hash()
function. And in example 2 we are getting the same error for using the dictionary object as a key for another dictionary.
Common Example Scenario
We mostly get this error when we use a dictionary object as a key for another dictionary. Let's say we have a list
requirement
that contains multiple dictionary items of electronic devices. Each dictionary item has two elements the item name and its quantity. And we need to create a new dictionary
priority
that contains only those list items whose quantity is more than 200.
Example
requirement = [ {"Name": "LEDs",
"Quantity": 250
},
{"Name": "Electric Tape",
"Quantity": 500
},
{"Name": "Fuse",
"Quantity": 100
},
{"Name": "Moters",
"Quantity": 10
},
{"Name": "Switches",
"Quantity": 100
},
{"Name": "Wire(100M)",
"Quantity": 500
}
]
# priority dictionary
priority = dict()
for item in requirement:
# item with quantity greater than 200
if item["Quantity"] > 200:
priority[item] = item["Quantity"] #error
print("The priority items are")
print(priority)
Output
Traceback (most recent call last):
File "main.py", line 29, in
priority[item] = item["Quantity"]
TypeError: unhashable type: 'dict'
Break the Output
In the above example, we are receiving the error in line 29 with
priority[item] = item["Quantity"]
statement. In this only statement, we are getting the error because here, we are trying to pass the dictionary object as key data to the
priority
dictionary. In that statement, the value of
item
is a dictionary object. And we know we get the
TypeError: unhashable type: 'dict'
error when we try a dictionary object as a key for another dictionary.
Solution
To solve the above example, we need to ensure that we are not using a dictionary object as key data for our
priority
dictionary. Instead of using the
priority[item] = item["Quantity"]
statement we should use
priority[item["Name"]] = item["Quantity"]
.
requirement = [
{"Name": "LEDs",
"Quantity": 250
},
{"Name": "Electric Tape",
"Quantity": 500
},
{"Name": "Fuse",
"Quantity": 100
},
{"Name": "Moters",
"Quantity": 10
},
{"Name": "Switches",
"Quantity": 100
},
{"Name": "Wire(100M)",
"Quantity": 500
}
]
# priority dictionary
priority = dict()
for item in requirement:
# item with quantity greater than 200
if item["Quantity"] > 200:
priority[item["Name"]] = item["Quantity"] #solved
print("The priority items are")
print(priority)
Output
The priority items are
{'LEDs': 250, 'Electric Tape': 500, 'Wire(100M)': 500}
Now our code runs successfully without any error.
Conclusion
The Error statement
"TypeError: unhashable type: 'dict'"
is a common error that many Python developers encounter when they deal with dictionaries and nested dictionaries. The error arises in a Python program when we accidentally try to use a dictionary object as a key for another dictionary. Python dictionary hash its key data to the associated value pair, and to hash the key, it uses the hash() function. The hash function can only hash the immutable data type, and when it tries to hash the dictionary, it throws the
TypeError: unhashable type: 'dict'
error.
If you are still getting this error in your Python program, please share your code and query in the comment section. We will try to help you in debugging.
People are also reading:
Leave a Comment on this Post