TypeError: unhashable type: ‘list’ (Python)

Any object with a “hash” value in Python is referred to as a “hashable” object. Some hashable objects are strings, tuples, int, bool, etc. These hashable objects are immutable and never modify their value after initialization. In contrast, “unhashable ” objects are mutable and do not have a ” hash ” value such as a list, dict, and set. The “TypeError: unhashable type: ‘list’ ” arises when the unhashable object “list” is passed as the key of the dictionary or element of the set in Python. To resolve this error, various solutions are provided by Python.

This Python write-up will explain the possible reasons and respective solutions for the “unhashable type: list” error using the following content:

So, let’s get started!

Reason 1: Use List as Key in Dictionary

The main reason which causes the error “unhashable type: ‘list’” in Python is when the user uses a list as a key value in the dictionary. An example of this error is demonstrated in the below snippet:

The above snippet shows that a “TypeError” occurs when we use the “List” as a dictionary key.

Reason 2: Use List as an Element in a Set

The other reason which causes the stated error in Python programs is using a list as an element of the set. The error demonstration is shown in the below snippet:

The above snippet shows the “TypeError” because the “List” is used as an element of the set.

Solution 1: Convert List to Tuple

To resolve this error from the Python program, the list value must be converted into a tuple. The tuple is immutable and will not return any “unhashable” error during execution.

Code:

list_value = tuple(['student','name','age'])
#using list as key
dict_val = {list_value: 'Lily', 'Marks': 92}
print(dict_val)
#using list as an element of set
set_value = {list_value}
print(set_value)

In the above code:

  • The list is converted into a tuple using the built-in “tuple()” function.
  • The converted list value (tuple) is used as a dictionary key, and it is also utilized as an element of the set.
  • The created dictionary and set are demonstrated on the output with the help of the “print()” function.

Output:

The above output verified that the dictionary and set had been successfully created.

Solution 2: Convert List to JSON Strings

The “unhashable type: list” error can be resolved by converting the list value into JSON strings. The JSON strings will resolve the stated error because they are immutable and hashable. Let’s have look at the following code block:

Code:

import json

json_value = json.dumps(['student','name','age'])
#using list as key
dict_val = {json_value: 'Lily', 'Marks': 92}
print(dict_val)
#using list as an element of set
set_value = {json_value}
print(set_value)

In the above code:

  • The “json” module is imported at the start of the program to access the function “json.dumps()”.
  • The “json.dumps()” function accepts the list as an argument and converts the list into formatted strings.
  • The formatted string is passed as a key in the dictionary and an element to the set.

Output:

The above snippet verified that the dictionary and set value were successfully created.

How to Check if an Object is Hashable or Not in Python?

To verify whether an object is hashable or not in Python, use the program’s built-in “hash()” function. The below code example will help you understand this concept better:

The hash() function will return some hash value if the object is hashable and an error if the function is unhashable.

Code-1: (Checking String Object)

print(hash('String Value'))

In the above code, the “hash()” function takes the string value as an argument and returns the hash value of the function.

Output:

The above output shows the hashable value of the given string.

Code-2: (Checking List and Dict Object)

print(hash(['ALex', 'John']))
print(hash({'a': 7, 'b': 6}))

In the above code, the “hash()” function takes the list and dict object as an argument and returns the “unhashable” error because the input object is “unhashable”.

Output:

The above output verified that the “list” and “dict” object is “unhashable”.

Conclusion

The “TypeError: unhashable type: list” occurs in Python when the user initializes the list as a key in a dictionary or uses the list as an item of a set. To resolve this error in Python, you can convert the list into a tuple or convert the list into a “JSON” string using the “JSON” module. Furthermore, this article demonstrated how to use Python’s built-in “hash()” function to check whether an object is hashable. This Python Write-up covered all details regarding “TypeError: unhashable type: list” using various examples.