[Fixed] Python TypeError: unhashable type: ‘list’

Unhashable types such as ‘list’ typically result in TypeError: unhashable type: ‘list’ errors. TypeError: unhashable type: ‘list’ occurs when a list is used as a key in the dictionary.

TypeError: unhashable type: ‘list’

Any unhashable object, such as a dictionary, will result in a TypeError: unhashable type: ‘dict’ being thrown when you attempt to add it to the key.

Any unhashable object, such as a list, will result in the TypeError: unhashable type: ‘list’ being thrown when you attempt to add it to the key.

An unordered collection is used to store data values in key: value pairs, the dictionary in Python is an unordered collection. When accessing and retrieving values from the dictionary, the key serves as an identifier for the value.

Only immutable hashable types such as strings, booleans, integers, and tuples are allowed in the keys, which means that the value will not change during the lifetime of the key. Python will be able to generate unique hash values for the keys as a result of this.

Code Example For Unhashable type:’list’ Error

This error indicates that the student key [4,5,6] is a list, not a hashable type, in Python, as indicated by the error. Dictionary keys must be immutable types, whereas the list is a mutable type, as specified by the specification.

#Initializing The String
student = {1:"Adam", 2:"Benny", 3:"Bella", [4,5,6]:"Chad"}

#Printing The Roll Number and Student Name
print(student)
Traceback (most recent call last):
  File "c:\Users\Coduber\test.py", line 2, in <module>
    student = {1:"Adam", 2:"Benny", 3:"Bella", [4,5,6]:"Chad"}
TypeError: unhashable type: 'list'
unhashable type

As you can see in the above code, I was able to successfully reproduce the error by using a list as a key in a dictionary. Let’s look at how we can fix this problem in the code example below.

How To Fix TypeError: unhashable type: ‘list’

There are a variety of approaches that can be used to resolve the unhashable type: ‘list’ error. Let’s take a closer look at each of these options individually.

1. Using Tuple Instead Of List

The quickest and most straightforward method of resolving this error is to convert the list into a tuple. Despite the fact that tuples and lists appear to be similar, they are frequently used for very different purposes.

Multiples are immutable and contain a heterogeneous sequence of elements that can be accessed through the use of unpacking or indexing operations on the elements.

As opposed to these structures, lists have elements that are homogeneous and can be accessed by iterating over the list of elements.

#Initializing The String
student = {1:"Adam", 2:"Benny", 3:"Bella", (4,5,6):"Chad"}

#Printing The Roll Number and Student Name
print(student)

Output:

{1: 'Adam', 2: 'Benny', 3: 'Bella', (4, 5, 6): 'Chad'}

2. By Removing The List As A Key In Dictionary

Because Python does not allow hashing of list items in the key section of the dictionary, it is best to avoid using a list as a key in a dictionary in Python.

Let’s look at a simple code example where I removed the list as a key from a dictionary in Python and replaced it with a proper key value pair for the sake of this example.

#Initializing The String
student = {1:"Adam", 2:"Benny", 3:"Bella", 4:"Chad"}

#Printing The Roll Number and Student Name
print(student)

Output:

{1: 'Adam', 2: 'Benny', 3: 'Bella', 4: 'Chad'}

Wrap Up

Hashing is a computer science concept used to create high-performance, pseudo-random access data structures when large amounts of data must be stored and accessed quickly.

In the above case, the list is mutable objects and hence it does not come under the property of hashing. That is the reason you are getting the error TypeError: unhashable type: ‘list’. That is why using a tuple which is immutable object fixes this issue.

If you are still having problems with this, please let me know in the comments section and I will be happy to assist you. Also, please let me know if you have a better solution than the one discussed above.

Further Read:

  1. Python How To Add A New Line To A String
  2. Python PermissionError: [Errno 13] Permission denied Fix
  3. Python ValueError: could not convert string to float [Fix]
  4. Python pip: command not found Quick Fix
  5. Python Write Text File

Leave a Comment