Search code examples
pythonsettypeerrorset-difference

set.difference() raising TypeError: unhashable type 'list'


Basicly what I'm trying to achieve is finding the unique values in one list, I was told it would be easiest to achieve using set.difference() although it raises

  File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\discord\ui\view.py", line 359, in _scheduled_task
    await item.callback(interaction)
  File "G:\Project\ReactionRoles.py", line 152, in callback
    await self.view.add_reactions()
  File "G:\Project\ReactionRoles.py", line 114, in add_reactions
    reactions_to_push = list(set(self.reactions).difference(set(already_in)))
TypeError: unhashable type: 'list'

Code looks like this:

assert self.model is not None
assert self.reactions is not None
already_in = self.get_roles()
assert already_in is not None
reactions_to_push = list(set(self.reactions).difference(set(already_in)))
for role in reactions_to_push:
    await self.original_message.add_reaction(role[1])

I thought it was because the list was empty, but when I changed the code to return something random instead of empty within list it still does the same, and I have no idea what the cause could be


Solution

  • Do you store a list in one of your set-lists?

    something like:

    #ok
    set([1,2,3])
    {1, 2, 3}
    
    # Error [3] is not hashable
    set([1,2,[3]])
    Traceback (most recent call last):
      File "<input>", line 1, in <module>
    TypeError: unhashable type: 'list'