To fix TypeError: unhashable type: ‘dict’ error in Python, there are 2 solutions we have effectively tested: Using the correct dictionary key and Using the item() method. Follow the article to better understand.
Why does the error “Unhashable type: ‘dict’ ” in Python happen?
Before talking about the error, we have a few definitions for you: TypeError , Unhashable , dict.
What is TypeError?
The ‘ TypeError ‘ occurs when the data types are incompatible with the same function.
What is Unhashable?
‘Unhashable’ is a feature in Python used to determine if an object has a hash value.
An object is hashable if it has a hash value for the duration of its existence.
A hash value is a key for a dictionary or an element of a set.
What is dict?
‘Dict’ stands for dictionary in Python.
Dictionary in Python has the function to store values of data in ‘key: value’ pairs.
Dictionary is a collection that is ordered*, non-duplicate, and interchangeable.
NOTE: since version 3.7 the dictionaries are sorted
Example:
infor = { "name": "John", "age": "15", } print(infor)
Output:
{'name': 'John', 'age': '15'}
Dictionary consists of two components: key and value.
Key is an identifier to associate with a value. As stated above, only hash objects can be keys in a dictionary. Therefore, one dictionary cannot be used as a key in another dictionary.
To add a key to the dictionary, you must specify a valid key. For example, ” infor” is a valid key but { ” infor”: ” name”} is not.
An illustrative example of a false situation:
cars = [
{"brand": "Kia", "sold": 4 },
{"brand": "Toyota", "sold": 7},
{"brand": "Ford", "sold": 8}
]
soldMoreThanFour = {}
The list “cars” contains three dictionaries. Each dictionary contains ‘key’ and ‘value’. It is named “brand” and “sold”.
I then run a for loop through the list of “cars” and filter out the cars sold more than five times. When the search is complete, it will be added to the dictionary “sold_more_than_four “:
cars = [ {"brand": "FORD", "sold": 4}, {"brand": "TOYOTA", "sold": 8}, {"brand": "KIA", "sold": 9} ] soldMoreThanFour = {} for a in cars: if a["sold"] > 4: soldMoreThanFour[a] = a["sold"] print(a["name"] + " has been sold more than four times.") print(soldMoreThanFour)
Output:
TypeError: unhashable type: 'dict'
The program doesn’t work because the value ‘a’ is a dictionary in the “cars” list
How to fix this error?
Method 1: Using correct dictionary key
Change the wrong command: soldMoreThanFour[a] = a["sold"]
to correct command: soldMoreThanFour[a["brand"]] = a["sold"]
Example:
cars = [ {"brand": "FORD", "sold": 4}, {"brand": "TOYOTA", "sold": 8}, {"brand": "KIA", "sold": 9} ] soldMoreThanFour = {} for a in cars: if a["sold"] > 4: soldMoreThanFour[a["brand"]] = a["sold"] print(a["brand"] + " has been sold more than four times.") print(soldMoreThanFour)
Output:
TOYOTA has been sold more than four times.
KIA has been sold more than four times.
{'TOYOTA': 8, 'KIA': 9}
Method 2: Using the item() method
The item() method returns an object containing a dictionary’s key and value pairs, which are called view objects.
Syntax:
dictionary.items()
Example:
infor = { "weight": "45", "age": "14" } person = infor.items() infor["age"] = 17 print(person)
Output:
dict_items([('weight', '45'), ('age', 17)])
Summary
Read this article to find out the answer for error “TypeError: unhashable type: ‘dict’” in Python. If you have any questions about this issue, please leave a comment below. I will answer your questions.
Leave a Reply