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

I am trying to add a new element to a set using update() function, but it returns the following error: TypeError: 'int' object is not iterable

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

>>> a.update(6)

Traceback (most recent call last):

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

TypeError: 'int' object is not iterable

How to fix this error? 

1 Answer

+2 votes
by (351k points)
selected by
 
Best answer

When you want to add just one element to a set, you should use the add() function. If you want to use the update() function, you need to pass the new element as a set.

Here is an example:

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

>>> a.add(6)

>>> a

{1, 2, 3, 4, 5, 6}

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

>>> a.update({6})

>>> a

{1, 2, 3, 4, 5, 6}

>>> 


...