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

Fix TypeError: unhashable type: ‘list’ in Python

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]}

Fix the typerror unhashable list or dict in Pandas dataframes

There are several cases in which you will receive the unhashable type exception in pandas:

  1. 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:

hrdf.drop_duplicates()

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 drop duplicates will work
hrdf.drop_duplicates()
  1. When aggregating the data using a Groupby object: Assume our hrdf DataFrame, which we defined in the previous steps of this tutorial and contains lists in its language columns. The following code will trigger an exception:
hrdf.groupby('language')['salary'].mean()

We can fix it as we did before, that is – to covert the language column to strings and then aggregate our data

hrdf['language']  = (hrdf['language'].astype('string'))
hrdf.groupby('language')['salary'].mean()