How to solve the typeerror unhashable type ‘list’ error?

Fix TypeError: unhashable type: ‘list’ in Python and pandas

Python structures such as Dictionary or a pandas DataFrame or Series objects, require that each object instance is uniquely identified . Hash values are a numeric constructs that can’t change and thus allows to uniquely identify each object. Lists are mutable objects and can change over time, hence can’t be hashed. When trying to use a list as a Dictionary key, Python throws a typeerror. To correct this issue convert your list to a tuple or string as these are hashable types.

Unhashable type – list as Dictionary keys

Assume that you write the following code

interviews = {['month', 'year']: ['July-23', 'December-22', 'July-21', 'March-22', 'June-24'],
 'language': ['JavaScript', 'R', 'Python', 'R', 'R'],
 'salary': [143.0, 71.0, 149.0, 134.0, 88.0]}

The first key (marked in bold) is a list object. We learnt that lists are mutable and not hashable. Hence Python will throw a type error:

TypeError: unhashable type: 'list'

Solving the unhashable ‘list’ error

You are able to solve this issue you should modify the first key to be either a tuple or a string. In this case we will convert the first key to a tuple.

interviews = {('month', 'year'): ['July-23', 'December-22', 'July-21', 'March-22', 'June-24'],
 'language': ['JavaScript', 'R', 'Python', 'R', 'R'],
 'salary': [143.0, 71.0, 149.0, 134.0, 88.0]}

Fixing the typerror unhashable list in pandas

You might encountered this error also when trying to slice a pandas DataFrame. Let’s start by creating a simple DataFrame

import pandas as pd

month = ['July', 'December', 'July', 'March', 'June']
language = [['Java'], ['Java'], 'Javascript', 'R', 'R']
salary = [143.0, 71.0, 149.0, 134.0, 88.0]
interviews = dict(language = language, salary = salary)
hrdf = pd.DataFrame(data=interviews, index=month)

Let’s now go ahead and drop duplicated rows:


This will throw our type error. The reason is that in order to detect dups, pandas uses hash values. We know that lists can be hashed hence the error.

The fix? Simply use tuples or strings in your DataFrame column if you are using it for finding duplicates.

hrdf['language']  = (hrdf['language'].astype('string'))
# then dropduplicates will work

Next learning

How to solve the string doesn’t have an append attribute error?