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

Ian Mackinnon picture Ian Mackinnon · Oct 28, 2010 · Viewed 99.1k times · Source

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

Answer

SingleNegationElimination picture SingleNegationElimination · Oct 28, 2010

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