fix typeerror unshashable type list

How to Fix – TypeError unhashable type ‘list’

In Python, a TypeError is raised when an operation or function is applied to an object of an inappropriate type. One such error is the “TypeError: unhashable type: ‘list'” error. This error occurs when we try to use a list (which is an unhashable type object) in a place that requires a hashable type. In this tutorial, we will discuss the reasons behind this error and how to fix it.

fix typeerror unshashable type list

Understanding the TypeError: unhashable type: 'list' error

In Python, hashable types are those which have a hash value that remains constant throughout its lifetime. Hash values are unique identifiers assigned to objects by Python’s built-in hash() function. These hash values are used to quickly compare and access objects in dictionaries and sets.

On the other hand, unhashable types are those which do not have a constant hash value and cannot be used as keys in dictionaries or elements in sets. Examples of unhashable types include lists, sets, and dictionaries themselves.

To illustrate the difference between hashable and unhashable types, consider the following example:

# Hashable type (tuple)
my_tuple = (1, 2, 3)
print(hash(my_tuple))

Output:

529344067295497451
# Unhashable type (list)
my_list = [1, 2, 3]
print(hash(my_list))

Output:

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

Cell In[2], line 3
      1 # Unhashable type (list)
      2 my_list = [1, 2, 3]
----> 3 print(hash(my_list))

TypeError: unhashable type: 'list'

In the above example, we create a tuple my_tuple and a list my_list containing the same elements. When we try to hash the tuple using the built-in hash() function, we get a unique hash value. However, when we try to hash the list, we get a TypeError because lists are unhashable. This means that we cannot use lists as keys in dictionaries or elements in sets.

The TypeError: unhashable type: 'list' error occurs when we try to use a list in a place that requires a hashable type, for example, as a key to a dictionary or a value in a set. This error occurs because lists are mutable, which means that their contents can be changed. In Python, only immutable objects can be used as keys in a dictionary or as elements in a set. Here are some common scenarios in which this error occurs:

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

  • When we try to use a list as a key in a dictionary
  • When we try to use a list as an element in a set
# using a list as a key in a dictionary
my_dictionary = {
    "Name": "Jim",
    ["Age", "Department"]: [26, "Sales"]
}

Output:

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

Cell In[3], line 2
      1 # using a list as a key in a dictionary
----> 2 my_dictionary = {
      3     "Name": "Jim",
      4     ["Age", "Department"]: [26, "Sales"]
      5 }

TypeError: unhashable type: 'list'

In the above example, we’re trying to use the list ["Age", "Department"] as a key in the dictionary my_dictionary. You can see that we get the TypeError: unhashable type: 'list'.

# using a list as a an element in a set
my_set = set([1, 2, 3])
my_set.add([4, 5])

Output:

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

Cell In[4], line 3
      1 # using a list as a an element in a set
      2 my_set = set([1, 2, 3])
----> 3 my_set.add([4, 5])

TypeError: unhashable type: 'list'

In the above example, we’re trying to add the list [4, 5] as an element to the set my_set. Since lists are unhashable types, we get the error TypeError: unhashable type: 'list'.

Fixing the Error

To fix the TypeError: unhashable type: 'list', use a hashable type like a tuple instead of a list. For example, if you’re trying to use a list as a key in a dictionary, use a tuple instead. Similarly, if you’re trying to use a list as an element in a set, you can use a tuple or a frozen set instead.

Let’s revisit the examples from above and fix those errors.

# using a tuple as a key in a dictionary
my_dictionary = {
    "Name": "Jim",
    ("Age", "Department"): [26, "Sales"]
}

In the above example, we replaced the list ["Age", "Department"] with the tuple, ("Age", "Department") as a key in the dictionary. We don’t get an error on executing the above code. Notice that you can still use a list as a value in the dictionary but the keys must be hashable types.

# using a tuple as a an element in a set
my_set = set([1, 2, 3])
my_set.add((4, 5))

In the above example, we replaced the list [4, 5] with the tuple, (4, 5) as an element in the set. We don’t get an error on executing the above code.

In summary, you can take the following steps to fix the TypeError: unhashable type: 'list' error.

  1. Identify the code that is causing the error.
  2. Check if you are using a list as a key in a dictionary or as an element in a set.
  3. If you are using a list as a key in a dictionary, consider using a tuple instead. Tuples are immutable and can be hashed.
  4. If you are using a list as an element in a set, consider using a tuple instead.
  5. If you cannot use a tuple, consider using a different data structure that is immutable and can work as an adequate replacement for your list.

Conclusion

The “TypeError: unhashable type: ‘list'” error occurs when we try to use a list as a key in a dictionary or as an element in a set. This error can be fixed by converting the list into an immutable object such as a tuple, a string, or a frozenset. By following the steps mentioned in this tutorial, you can easily fix this error and make your code more efficient and error-free.

You might also be interested in –

Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top