set.update()
The in-place union — take any iterable(s) and add every element. The bulk cousin of add().
Demo
update mutates the set in place. Real Python returns None; the demo shows the RESULTING state so the effect is visible. Every element from the iterable that is not already present is added. Duplicates in the iterable collapse (they already do in a set). The iterable can be any iterable — a list, tuple, string, generator — not just another set.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| *others | iterable | no (()) | Zero or more iterables. Every element of each is added to the set. Iterables need not be sets — lists, tuples, generators, and strings all work. |
Return value
None — Returns None. The set is mutated in place — every element from each iterable that is not already present is added. Equivalent to `s |= set(other1) | set(other2) | ...` but takes any iterables, not just sets.
Common patterns
seen.update(new_batch)
names.update(first_names, last_names, aliases)
s |= other_set # requires a set on the right s.update(iterable) # any iterable works
s.update(f(x) for x in inputs if valid(x))
Examples
Pitfalls
s = {1, 2} s = s.update([3]) print(s)
s.update([3]) print(s)
before = s s.update(new) before is s
before = s.copy() s.update(new) before
s = set() s.update("hi")
s.update(["hi"])
s.update([[1, 2], [3, 4]])
s.update([(1, 2), (3, 4)])
When to use
- Adding a batch of elements from any iterable
- Merging many sets or lists into one
- Building up a set from a stream or generator
- Any time `for x in iterable: s.add(x)` would work — one call is faster and clearer
- You need a new set — use `|` or `union()` for a pure result
- One element at a time → s.add(x) is clearer
- Elements might be unhashable → filter or convert first
- You want the result assigned back — the operator form (|=) makes intent explicit
Notes
FAQ
For a set on the right, they are equivalent: `s |= t` calls `s.update(t)`. But update accepts ANY iterable (list, string, generator) — the `|=` operator requires a set. update also accepts multiple iterables in one call.