How to Fix Python TypeError: Unhashable Type: List

Zeeshan Afridi Feb 02, 2024
  1. the TypeError: unhashable type: 'list' in Python
  2. Hash Function in Python
  3. Fix the TypeError: unhashable type: 'list' in Python
How to Fix Python TypeError: Unhashable Type: List

This article will discuss the TypeError: unhashable type: 'list' and how to fix it in Python.

the TypeError: unhashable type: 'list' in Python

This error occurs when you pass the unhashable objects like a list as a key to the Python dictionary or find a function’s hash value.

Dictionaries is a data structure in Python that works in key-value pairs, every key has a value against it, and to access the values of values, you will need the keys like array indices.

Syntax of a dictionary:

dic = {"key": "Values"}

Hashable objects are those whose value doesn’t change over time but remain the same tuples and strings are types of hashable objects.

Code:

# creating a dictionary
dic = {
    # list as a key --> Error because lists are immutable
    ["a", "b"]: [1, 2]
}

print(dic)

Output:

TypeError: unhashable type: 'list'

We have used a list ["a","b"] as the key, but the compiler has thrown a TypeError: unhashable type: 'list'.

Let’s manually find the hash value of the list.

Code:

lst = ["a", "b"]

hash_value = hash(lst)

print(hash_value)

Output:

TypeError: unhashable type: 'list'

The hash() function is used to find the hash value of a given object, but the object must be immutable like string, tuple, etc.

Hash Function in Python

The hash() function is an encryption technique that encrypts the immutable object and assigns a unique value to it, known as the object’s hash value. No matter the data’s size, it provides the same size of unique value.

Code:

string_val = "String Value"
tuple_val = (1, 2, 3, 4, 5)
msg = """Hey there!
              Welcome to DelfStack"""

print("Hash of a string object\t\t", hash(string_val))
print("Hash of a tuple object\t\t", hash(tuple_val))
print("Hash of a string message\t", hash(tuple_val))

Output:

Hash of a string object      -74188595
Hash of a tuple object       -1883319094
Hash of a string message     -1883319094

The hash values are the same size and unique for every value.

Fix the TypeError: unhashable type: 'list' in Python

To fix the TypeError in Python, you have to use immutable objects as the keys of dictionaries and as arguments to the hash() function. Notice in the above code the hash() function works perfectly with mutable objects like tuple and string.

Let’s see how we can fix the TypeError: unhashable type: 'list' in dictionaries.

Code:

# creating a dictionary
dic = {
    # string as key
    "a": [1, 2]
}

print(dic)

Output:

{'a': [1, 2]}

This time we provide a string "a" as the key, which is fine to work with because strings are mutable.

Zeeshan Afridi avatar Zeeshan Afridi avatar

Zeeshan is a detail oriented software engineer that helps companies and individuals make their lives and easier with software solutions.

LinkedIn

Related Article - Python Error

Related Article - Python TypeError