Python Glossary Русский

all Python 3Sets (set)function

set.update

adding

Add elements from one or more iterables in place (in-place union).

Syntax

s.update(*iterables)

The argument may be any iterable, which is exactly where students trip: a string is split into its characters instead of being added whole — use add() for a single element. The |= operator does the same job but insists on a set on the right, while update() happily swallows lists, tuples or generators, several of them in one call.

Examples

s = {1, 2}
s.update({3, 4}, {5})
print(sorted(s))   # → [1, 2, 3, 4, 5]

See also

Open in the full glossarydocs.python.orgReport an inaccuracy