Blog |

How to Handle Unhashable Type List Exceptions in Python

How to Handle Unhashable Type List Exceptions in Python
Table of Contents

The Python TypeError: unhashable type: 'list' usually means that a list is being used as a hash argument. This error occurs when trying to hash a list, which is an unhashable object. For example, using a list as a key in a Python dictionary will cause this error since dictionaries only accept hashable data types as a key.

The standard way to solve this issue is to cast a list to a tuple, which is a hashable data type.

Install the Python SDK to identify and fix these undefined errors

Tuples vs Lists

Tuples are similar to lists but are immutable. They usually contain a heterogeneous sequence of elements that are accessed via unpacking or indexing. On the other hand, lists are mutable and contain a homogeneous sequence of elements that are accessed by iterating over the list.

Immutable objects such as tuples are hashable since they have a single unique value that never changes. Hashing such objects always produces the same result, so they can be used as the keys for dictionaries.


TypeError: Unhashable Type: 'List' Example

Here’s an example of a Python TypeError: unhashable type: 'list' thrown when a list is used as the key for a dictionary:

my_dict = {1: 'Bob', [2,3,4]: 'names'}

Since a list is not hashable, running the above code produces the following error:

Traceback (most recent call last):
  File "", line 1, in <module>
    my_dict = {1: 'Bob', [2,3,4]: 'names'}
TypeError: unhashable type: 'list'


How to Fix TypeError: unhashable type: 'list'

The Python TypeError: Unhashable Type: 'List' can be fixed by casting a list to a tuple before using it as a key in a dictionary:

my_dict = {1: 'Bob', tuple([2,3,4]): 'names'}

In the example above, the tuple() function is used to convert the list to a tuple. The above code runs successfully, produces the following output:

{1: 'Bob', (2, 3, 4): 'names'}


Frequently Asked Questions

What are Unhashable Type Errors in Python?

Unhashable type errors appear in a Python program when a data type that is not hashable is used in code that requires hashable data. An example of this is using an element in a set or a list as the key of a dictionary.

What is Hashable in Python?

Hashable is a feature of Python objects that determines whether the object has a hash value or not. An object is hashable if it has a hash value that doesn't change during its lifetime. A hashable object can be used as a key for a dictionary or as an element in a set.

All built-in immutable objects, like tuples, are hashable while mutable containers like lists and dictionaries are not hashable.


Track, Analyze and Manage Errors With Rollbar

Rollbar in action

Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you to proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing Java errors easier than ever. Sign Up Today!

Related Resources

"Rollbar allows us to go from alerting to impact analysis and resolution in a matter of minutes. Without it we would be flying blind."

Error Monitoring

Start continuously improving your code today.

Get Started Shape