+1 vote
in Programming Languages by (56.8k points)

When I try to add a list to a set using add() function, it gives error: "TypeError: unhashable type: 'list'". How to fix this error?

E.g.

>>> a={2,3,4}

>>> b=[8,9]

>>> a.add(b)

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: unhashable type: 'list'

1 Answer

+1 vote
by (351k points)
selected by
 
Best answer

The add() function adds one element to a set. Since you are trying to add a list of elements, you should use the update() function instead of the add() function. It should work without any errors.

Here is an example:

>>> a={2,3,4}

>>> b=[8,9]

>>> a.update(b)

>>> a

{2, 3, 4, 8, 9}

Related questions


...