Fix Python – How do I add the contents of an iterable to a set?

Question

Asked By – Ian Mackinnon

What is the “one […] obvious way” to add all items of an iterable to an existing set?

Now we will see solution for issue: How do I add the contents of an iterable to a set?


Answer

You can add elements of a list to a set like this:

>>> foo = set(range(0, 4))
>>> foo
set([0, 1, 2, 3])
>>> foo.update(range(2, 6))
>>> foo
set([0, 1, 2, 3, 4, 5])

This question is answered By – SingleNegationElimination

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0