In Python, a TypeError is raised when an operation or function is applied to an object of inappropriate type. One such error is the “TypeError: unhashable type: ‘dict'” error. This error occurs when we try to use a dictionary as a key in another dictionary or as an element in a set. In this tutorial, we will discuss the reasons behind this error and how to fix it.
Understanding the TypeError: unhashable type: 'list'
error
In Python, hashable types are those which have a hash value that remains constant throughout its lifetime. Hash values are unique identifiers assigned to objects by Python’s built-in hash()
function. These hash values are used to quickly compare and access objects in dictionaries and sets.
On the other hand, unhashable types are those which do not have a constant hash value and cannot be used as keys in dictionaries or elements in sets. Examples of unhashable types include lists, sets, and dictionaries themselves.
To illustrate the difference between hashable and unhashable types, consider the following example:
# Hashable type (tuple) my_tuple = (1, 2, 3) print(hash(my_tuple))
Output:
529344067295497451
# Unhashable type (dict) my_dict = {'Name': 'Jim', 'Age': 26} print(hash(my_dict))
Output:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[2], line 3 1 # Unhashable type (dict) 2 my_dict = {'Name': 'Jim', 'Age': 26} ----> 3 print(hash(my_dict)) TypeError: unhashable type: 'dict'
In the above example, we create a tuple my_tuple
and a dictionary my_dict
. When we try to hash the tuple using the built-in hash()
function, we get a unique hash value. However, when we try to hash the dictionary, we get a TypeError
because dictionary objects are unhashable. This means that we cannot use dictionaries as keys in other dictionaries or elements in sets.
The TypeError: unhashable type: 'dict'
error occurs when we try to use a dict object in a place that requires a hashable type, for example, as a key to a dictionary or a value in a set. This error occurs because dictionaries are mutable, which means that their contents can be changed. In Python, only immutable objects can be used as keys in a dictionary or as elements in a set. Here are some common scenarios in which this error occurs:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
- When we try to use a dictionary as a key in another dictionary
- When we try to use a dictionary as an element in a set
# using a dictionary as a key in another dictionary address = { 'City': 'Houston', 'State': 'Texas' } employee_details = { 'Name': 'Jim', 'Age': 26, address: 'Houston, Texas' }
Output:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[3], line 7 1 # using a dictionary as a key in another dictionary 2 address = { 3 'City': 'Houston', 4 'State': 'Texas' 5 } ----> 7 employee_details = { 8 'Name': 'Jim', 9 'Age': 26, 10 address: 'Houston, Texas' 11 } TypeError: unhashable type: 'dict'
In the above example, we’re trying to use the dict address
as a key in the dictionary employee_details
. You can see that we get the TypeError: unhashable type: 'dict'
.
# using a list as a an element in a set my_set = set([1, 2, 3]) my_dict = {'foo': 'bar'} my_set.add(my_dict)
Output:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[4], line 4 2 my_set = set([1, 2, 3]) 3 my_dict = {'foo': 'bar'} ----> 4 my_set.add(my_dict) TypeError: unhashable type: 'dict'
In the above example, we’re trying to add the dict my_dict
as an element to the set my_set
. Since dictionaries are unhashable types, we get the error TypeError: unhashable type: 'dict'
.
Fixing the Error
To fix the TypeError: unhashable type: 'dict'
, do not use a dictionary in places that require a hashable type. For example, as a key to another dictionary or as an element in a set.
Let’s revisit the example from above and fix the error.
# using a dictionary as a key in another dictionary address = { 'City': 'Houston', 'State': 'Texas' } employee_details = { 'Name': 'Jim', 'Age': 26, 'Address': address } print(employee_details)
Output:
{'Name': 'Jim', 'Age': 26, 'Address': {'City': 'Houston', 'State': 'Texas'}}
In the above example, we’re not using a dictionary as a key in the dictionary employee_details
instead we are using a string, ‘Address’ (which is hashable type) and setting its value to the dictionary address
. Note that you can still use a dictionary as a value in another dictionary but the keys must be hashable types only.
Conclusion
The “TypeError: unhashable type: ‘dict'” error occurs when we try to use a dictionary as a key in another dictionary or as an element in a set. This error can be fixed by using a hashable type instead of a dictionary. By following the steps mentioned in this tutorial, you can easily fix this error and make your code more efficient and error-free.
You might also be interested in –