The error TypeError: unhashable type: ‘list’ occurs when trying to get a hash of a list. For example, using a list as a key in a Python dictionary will throw the TypeError because you can only use hashable data types as a key.
To solve this error, you can cast the list to a tuple, which is hashable.
This tutorial will go through the error in detail and how to solve it with the help of code examples.
Table of contents
TypeError: unhashable type: ‘list’
What Does TypeError Mean?
TypeError occurs whenever you try to perform an illegal operation for a specific data type object. In the example, the illegal operation is hashing, and the data type is List.
What Does Unhashable Mean?
By definition, a dictionary key needs to be hashable. An object is hashable if it has a hash value that remains the same during its lifetime. A hash value is an integer Python uses to compare dictionary keys while looking at a dictionary.
When we add a new key:value pair to a dictionary, the Python interpreter generates a hash of the key.
Similarly, we can think of a set as a dictionary that just contains the keys, so it also requires hashable items.
We can only hash particular objects in Python, like strings or integers. All immutable built-in objects in Python are hashable, for example, tuple, and mutable containers are not hashable, for example, list.
Example #1: Using a List as a Key in a Dictionary
Let’s look at an example where we define a dictionary. The first two keys are strings, the third key is a list of strings, and the values are integers.
name_list = ["Tim", "Lisa"]
a_dict = {
"Alex": 2,
"Holly":4,
name_list:6
}
print(a_dict)
If we try to create the dictionary, we will get the following error:
TypeError Traceback (most recent call last)
----≻ 1 a_dict = {
2 "Alex": 2,
3 "Holly":4,
4 name_list:6
5 }
TypeError: unhashable type: 'list'
This error occurs because only hashable objects can be a key in a dictionary, and lists are not hashable objects.
Solution
To solve this error, we can cast the list to a tuple before using it as a dictionary, using the built-in tuple() method. Let’s look at the revised code:
name_list = ["Tim", "Lisa"]
a_dict = {
"Alex": 2,
"Holly":4,
tuple(name_list):6
}
print(a_dict)
{'Alex': 2, 'Holly': 4, ('Tim', 'Lisa'): 6}
You can also unpack the list into its elements and use those objects as keys. Let’s look at the revised code:
name_list = ["Tim", "Lisa"]
key3, key4 = name_list
a_dict = {
"Alex": 2,
"Holly":4,
key3:6,
key4:6
}
print(a_dict)
In this example, we set the value for the keys ‘Tim’ and ‘Lisa’ to 6. Let’s run the code to see the result:
{'Alex': 2, 'Holly': 4, 'Tim': 6, 'Lisa': 6}
Example #2: Using a List as a Value in a Set
If we try to cast a list to a set and one of the values in the list is itself a list, we will throw the TypeError. Let’s look at a code example:
a_list = [1, 2, [3, 4], 5, 6]
a_set = set(a_list)
print(a_set)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
----≻ 1 a_set = set(a_list)
TypeError: unhashable type: 'list'
This error occurs because sets require their items to be hashable. Only the immutable types like numbers are hashable out of the predefined Python types. But the list [3, 4] is mutable, so we cannot include it in the set.
Solution #1: Cast List to Tuple
We can cast the list item to a tuple before casting the full list to a set to solve this error. Let’s look at the revised code:
a_list = [1, 2, tuple([3, 4]), 5, 6]
a_set = set(a_list)
print(a_set)
Let’s run the code to get the result:
{1, 2, 5, (3, 4), 6}
Summary
Congratulations on reading to the end of this article! The error: TypeError: unhashable type: ‘list’ occurs when trying to get the hash value of a list.
If you want to get the hash of a container object, you should cast the list to a tuple before hashing.
For further reading on TypeError with unhashable data types, go to the article: How to Solve Python TypeError: unhashable type: ‘numpy.ndarray’.
Go to the Python online courses page to learn more about coding in Python for data science and machine learning.
Have fun and happy researching!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.