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
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.
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:
- You might encountered this error also when trying to slice a pandas DataFrame. Let’s start by creating a simple DataFrame
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 drop duplicates will work hrdf.drop_duplicates()
- 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:
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()