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

StackOverflow

What is the "one [...] obvious way" to add all items of an iterable to an existing set?

Answer rating: 248

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])




Tutorials