Python TypeError: Unhashable Type: List

Python TypeError: Unhashable Type: List

  1. the TypeError: unhashable type: 'list' in Python
  2. Hash Function in Python
  3. Fix the TypeError: unhashable type: 'list' in Python

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.


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



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.


lst = ["a","b"]

hash_value = hash(lst)



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.


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))


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.


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



{'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.


Related Article - Python Error

  • Python PermissionError: [WinError 5] Access Is Denied
  • Python TypeError: 'DataFrame' Object Is Not Callable
  • Python TypeError: Can't Convert 'List' Object to STR
  • Local Variable Referenced Before Assignment Error in Python
  • Python Handling Socket.Error: [Errno 104] Connection Reset by Peer
  • Python Is Not Recognized in Windows 10
  • Related Article - Python TypeError

  • TypeError: Iteration Over Non-Sequence
  • TypeError: Must Be Real Number, Not STR
  • Solve the TypeError: An Integer Is Required in Python
  • Solve the TypeError: Not All Arguments Converted During String Formatting in Python
  • Python TypeError: Missing 1 Required Positional Argument