Home > Software engineering >  How to add MULTIPLE strings to a set?
How to add MULTIPLE strings to a set?

Time:02-10

I have a set "y"

I can add one string no problem, but here is the input/output when I add multiple strings:

y.update(['tam','sam'])
print(y)

{'sam', 'baz', 'a', 'tam', 's', 'bar', 'jas', 'foo', 't', 'm'}

I created a list in the argument for update because I thought it is not iterable. Any thoughts/solutions?

CodePudding user response:

I think you can use tuple y.update(('tam','sam'))

CodePudding user response:

You can convert your set to list, add the new list and then convert it back to set.

y = {'sam', 'baz', 'a', 'tam', 's', 'bar', 'jas', 'foo', 't', 'm'}

y = list(y)
y.extend(['ron', 'harry'])

y = set(y)
print(y)
{'bar', 'sam', 'jas', 'm', 'ron', 'tam', 's', 'baz', 't', 'a', 'foo', 'harry'}
  •  Tags:  
  • Related