The TypeError: unhashable type: ‘list’ error occurs when you try to add a list as a key in a dictionary or as an item in a python Set. A hashable object is used as a key in the dictionary. The list is an unhashable type object that you’ve been trying to store. If an unhashable type object is tried to hash, the error TypeError: unhashable type: ‘list’ is thrown. The python set and dictionary uses hash algorithm to create a hash key which is used to get data quickly.
The list is a mutable object which can be modified at any time. If the list is modified after the hash key has been generated, then, the hash algorithm can not recognize the list. Hence the objects that are mutable are not hashable. If a list is passed as a hash argument, then it will throw the error TypeError: unhashable type: ‘list’.
Exception
This will show the stack trace of the error as below. The TypeError: unhashable type error is shown as
Traceback (most recent call last):
File "/Users/python/Desktop/test.py", line 1, in <module>
a = {1, [2,3] , 4}
TypeError: unhashable type: 'list'
[Finished in 0.1s with exit code 1]
How to reproduce this issue
The python list is an object which is not hashable. In python, create a list object. Add the list into a set object. Or, add the list object as a key in the python dictionary. Since the unhashable object is not permitted in set or dictionary key, the error will be thrown.
test.py
a = {1, [2,3] , 4}
print a
Output
Traceback (most recent call last):
File "/Users/python/Desktop/test.py", line 1, in <module>
a = {1, [2,3] , 4}
TypeError: unhashable type: 'list'
[Finished in 0.1s with exit code 1]
Root Cause
The set or dictionary uses hashing algorithm to store and retrieve the elements in the data structure. The set maintains the unique objects with hash code. The dictionary maintains the unique keys to store values. The list is an unhashable object that can be modified at any given time. The error TypeError: unhashable type: ‘list’ is due to the addition of an unhashable object to the set or dictionary key.
Solution 1
The unhashable object list can be stored in the dictionary after the list is converted to tuple. The tuple is a immutable object that can be stored in dictionary or set. The tuple() function is used to convert the list to a tuple. This will resolve the python error “TypeError: unhashable type: ‘list’”
test.py
a = {1, tuple([2,3]) , 4}
print a
Output
set([1, (2, 3), 4])
[Finished in 0.1s]
Solution 2
The python error “TypeError: unhashable type: ‘set’” is thrown if the set is added as a key in the dictionary. The set isn’t an immutable object. If the set object is to be converted as a tuple before it is added to the dictionary. This will resolve the “TypeError: unhashable type: ‘set’” error.
Example
a = {1:2, tuple([2,3]):3 , 4:4}
print a
Output
{1: 2, (2, 3): 3, 4: 4}
[Finished in 0.1s]
Solution 3
The another option to add the list in a dictionary is to store as value. The example below illustrates how to add a list into a dictionary as a value. In a dictionary the mutable objects can be stored as a value
Example
a = {'a':1, 'b':[2,3] , 'c':4}
print a
Output
{'a': 1, 'c': 4, 'b': [2, 3]}
[Finished in 0.0s]