update(*others) adds every item of each iterable you pass to the set. It changes the set in place, skips values that are already present, and returns None.
The arguments can be any iterable: a list, a tuple, another set, a range, a dict (its keys), or a string (its single characters). You may pass several at once, or none, in which case nothing happens.
set.update() is an instance method of the built-in set type: call it on a set with dot notation, such as seen.update(...). No import is needed.
Syntax
Pass zero or more iterables, separated by commas; the operator form needs a set on the right.
python
s.update(iterable)
s.update(iterable1, iterable2, iterable3)
s |= other_set
Call
What it does
{1, 2}.update([2, 3, 4])
Returns None; the set becomes {1, 2, 3, 4}. The duplicate 2 is ignored.
{1, 2}.update(range(3, 5), (6,))
Returns None; the set becomes {1, 2, 3, 4, 6}. Several iterables can be passed at once.
{1, 2}.update(5)
Raises TypeError: an int is not iterable.
s |= other works only when other is a set, while update() takes any iterable. To add exactly one item use add(); to get a new set instead of changing this one use union().
Method signatures
python
set.update(*others)
# returns: None; every item of every iterable is now in the set
# raises: TypeError if an argument is not iterable or an item is unhashable
Parameters
Parameter
Description
*others
Zero or more iterables: list, tuple, set, string, range, dict, and so on. Every item must be hashable. A dict contributes its keys, and a string contributes its characters.
Return Value
Return Value
Description
None
Always None. The set itself now holds its old items plus every item from the arguments.
Python Examples
Example 1 — Merge Email Lists Without Duplicates
Feed a list and a tuple into one set with a single call.