TypeError: unhashable type: ‘list’

In this post, we will discuss the following python errors.

  1. TypeError: unhashable type: ‘list’
  2. TypeError: unhashable type: ‘dict’
  3. TypeError: unhashable type: ‘set’

The python error “TypeError: unhashable type: ‘list'” occurs when a list is added as a dictionary or set key. The list is a unhashable object which can not be added as a key. The dictionary hashes the object and uses the hash value as a primary reference to the key.

The list is a mutable object that can be modified any time. The hash value changes when the object is modified. Hashing is an algorithm that allows you to store object which can be retrieved quickly when it is needed. The object can not be retrieved in the dictionary if the hash value is changed



Exception

If a list is added as a dictionary or set key, the error “TypeError: unhashable type: ‘list'” is thrown in python. The error will be shown as below.

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 list is a mutable object that can be modified at any time. The mutable object can not be stored as a key in the dictionary or set as the hash value of the key is used as an identifier to retrieve the key. The hash value changes as the object is changed. Thus, the unhashable object list can not be stored in the dictionary or set.



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'”

Example

a = {1, [2,3] , 4}
print a

Exception

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]

Solution

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}
b = {3 , 4}
c = {a:1, b:2}
print c[a]

Exception

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 3, in <module>
    c = {a:1, b:2}
TypeError: unhashable type: 'set'
[Finished in 0.1s with exit code 1]

Solution

a = tuple({1 , 2})
b = tuple({3 , 4})
c = {a:1, b:2}
print c[a]

Output

1
[Finished in 0.0s]


Solution 3

The dictionary can not be added as a key of another dictionary. The dictionary is not an immutable object. The dictionary is to be converted to tuple before storing in to another dictionary. The immutable objects can be stored in the dictionary. This will resolve the error “TypeError: unhashable type: ‘dict'”

Example

a = {1:1, 2:2 , 3:3}
b = {a:1, 2:3}
print b

Exception

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 2, in <module>
    b = {a:1, 2:3}
TypeError: unhashable type: 'dict'
[Finished in 0.1s with exit code 1]

Solution

a = tuple({1:1, 2:2 , 3:3})
b = {a:1, 2:3}
print b

Output

{2: 3, (1, 2, 3): 1}
[Finished in 0.1s]



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *