Python dictionaries only accept hashable data types as a key in a dictionary. A list is not a hashable data type. If you specify a list as a key in a dictionary, you’ll encounter a “TypeError: unhashable type: ‘list’” error.
In this guide, we talk about what this error means and why you see it in your code. We’ll walk through an example of this error to show you how to solve it.
TypeError: unhashable type: ‘list’
Dictionaries have two parts: keys and values. Keys are the labels associated with a particular value. To access a value, you must reference that value’s key name.
While values can be of any data type, from lists to strings, only hashable objects are acceptable as keys. Hashable objects are objects with a hash value that does not change over time. Examples of hashable objects are tuples and strings.
Lists do not have an unchanging hash value. Their hash values can change over time. This means you cannot specify a list as a dictionary key.
An Example Scenario
Let’s build a program that creates a list of students who have an average grade of over 75. This list will contain dictionary records of each student and their grades. To create this dictionary, we’ll work from a dictionary with a list of all the students in a school and their grades.
Let’s start by defining a list of students and a dictionary for our top students:
students = [ { "name": "Linda", "grades": [84, 82, 65] }, { "name": "Alex", "grades": [67, 68, 83] }, { "name": "Holly", "grades": [72, 74, 81] } ] top_students = {}
Each value in the “students” list is a dictionary. Each dictionary contains two keys: name and grades. We have defined a dictionary called “top_students” which will contain the information about our top students.
Now that we have defined this dictionary, we use a for loop to filter out students whose average grades are over 75 and add them to our new dictionary:
for s in students: average = sum(s["grades"]) / len(s["grades"]) if average > 75: top_students[s["grades"]] =s["name"]
In each iteration of the for loop, we calculate the average of all the grades a student has earned. We do this by dividing the total of all grades by how many grades have been recorded.
Next, we check if that average is greater than 75. If it is, we create a new entry in the top_students dictionary with the name of a student and their grades.
Finally, we print the top_students dictionary to the console so we see all the students who have an average grade of over 75:
print(top_students)
Run our code and see what happens:
Traceback (most recent call last): File "main.py", line 11, in <module> top_students[s["grades"]] = s["name"] TypeError: unhashable type: 'list'
Our code fails to execute successfully.
The Solution
Our code works up until the interpreter reaches line 11. On this line, our code states:
top_students[s["grades"]] = s["name"]
The error in our code is because we’ve tried to assign a list as a key in a dictionary. When our code parses this line on the first iteration of our loop, our code tries to create a dictionary with the following key and value:
{ [84, 82, 65]: "Linda" }
This is an invalid dictionary. Our code tries to assign a list as a key which does not work.
To solve this problem, we use the student’s name as a key in the dictionary instead of the list of grades:
top_students[s["name"]] = s["grades"]
We’ve assigned the list of grades as a value instead of as a key. The student’s name is the key in the dictionary. Let’s run our code:
{'Linda': [84, 82, 65], 'Holly': [72, 74, 81]}
Our code successfully creates a dictionary with information about the top-performing students. Any student whose average grade on their last three tests is over 75 was added to the dictionary.
Our dictionary breaks down as follows:
- Keys: Linda, Holly
- Values: [84, 82, 65], [72, 74, 81]
Because we are now assigning strings to our key names, our code works.
Conclusion
The “TypeError: unhashable type: ‘list’” error is raised when you try to assign a list as a key in a dictionary. To solve this error, ensure you only assign a hashable object, such as a string or a tuple, as a key for a dictionary.
Now you’re ready to solve this error like a professional coder!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.