I have the following dataframe comments. I have segregated a list of users based on certain conditions. I want to get the count of words based on those users which are named as gold_users. But I am getting an error in my code TypeError: unhashable type: 'list'. Please help me fix this.


       Id|                    Text                             |    UserId  
        6|  [2006, course, allen, knutsons, 2001, course, ...  |    3   
        8|  [also, theo, johnsonfreyd, note, mark, haimans...  |    1


for index,rows in comments.iterrows():
  gold_comments = rows[comments.Text.loc[comments.UserId.isin(gold_users)]]

Expected Output

#Top 10 Words that appear the most in the comments made by gold users with their count.
TypeError: unhashable type: 'list'.

You will get this error when you are trying to put list as key in dictionary or set because list is unhashable object.

Example you trying to input code such as

dict1 ={ 1:'one', [2]:'two'}

TypeError Traceback (most recent call last)
<ipython-input-73-c4e2fd1e6bf0> in <module>
----> 1 dict1 ={ 1:'one', [2]:'two'}
      2 print(dict1)
TypeError: unhashable type: 'list'

solution : Try to convert the list into tuple

Hashable object in Python:

  • int
  • float
  • decimal
  • bool
  • string
  • tuple
  • complex
  • range
  • frozenset
  • bytes

UnHashable object in Python

  • list
  • set
  • dict
  • bytearray
  • customclasses

Not the answer you're looking for? Browse other questions tagged or ask your own question.