set.update()
set.update(*others) None · Updated March 15, 2026 · Set Methods The update() method adds all elements from one or more iterables to a set. Unlike methods like union() that return a new set, this method modifies the original set directly. It accepts any number of iterables as arguments and silently ignores duplicate values.
Syntax
set.update(*others)
Parameters
| Parameter | Type | Description |
|---|---|---|
*others | iterable | Any number of iterables (sets, lists, tuples, strings, dicts, etc.) whose elements will be added to the set. |
Examples
Basic usage
Add elements from another set:
s = {1, 2, 3}
s.update({4, 5})
print(s) # {1, 2, 3, 4, 5}
From a list or tuple
You can add elements from any iterable:
s = {"a", "b"}
s.update(["c", "d"])
print(s) # {"a", "b", "c", "d"}
s.update(("e", "f"))
print(s) # {"a", "b", "c", "d", "e", "f"}
Using multiple arguments
Add elements from several iterables at once:
s = {1}
s.update([2, 3], (4, 5), {6})
print(s) # {1, 2, 3, 4, 5, 6}
This is equivalent to calling update() multiple times or using the |= operator.
With a dictionary
When updating with a dictionary, only the keys are added:
s = {"a"}
s.update({"b": 1, "c": 2})
print(s) # {"a", "b", "c"}
Duplicates are ignored
Only unique elements are added:
s = {1, 2}
s.update([2, 3, 3, 3])
print(s) # {1, 2, 3}
Using the |= operator
The |= operator is equivalent to update():
s = {1, 2}
s |= {3, 4}
print(s) # {1, 2, 3, 4}
Common Patterns
Merging collections
A common pattern is merging multiple data sources:
initial = {"user1", "user2"}
new_users = ["user3", "user4"]
removed_users = {"user1"}
initial.update(new_users)
for u in removed_users:
initial.discard(u)
print(initial) # {"user2", "user3", "user4"}
Building a set from user input
Collect unique values from various input sources:
tags = set()
tags.update(input("Enter tags: ").split())
tags.update(input("More tags: ").split())
print(sorted(tags))
Performance
The update() method:
- Time complexity: O(len(others)) where others is the total number of elements across all iterables
- Space complexity: O(n) for the elements being added
- Each element is hashed once, making lookups efficient
For combining sets without mutating the original, use set.union() instead.
Return Value
This method returns None — it modifies the set in place. This is different from union() which returns a new set.
See Also
- set-methods:set-union — returns a new set with combined elements
- set-methods:set-add — adds a single element to a set
- set-methods:set-discard — removes elements without raising errors