Python Set update() Method
Example
Insert the items from set y into set
x:
y = {"google", "microsoft", "apple"}
x.update(y)
print(x)
Definition and Usage
The update() method updates the current set,
by adding items from another set (or any other iterable).
If an item is present in both sets, only one appearance of this item will be present in the updated set.
As a shortcut, you can use the |= operator instead, see example below.
Syntax
Parameter Values
| Parameter | Description |
|---|---|
| set1 | Required. The iterable insert into the current set |
| set2 | Optional. More iterables to insert into the current set. You can insert as many iterables as you like. Separate each iterable with a comma. |
Shorter Syntax
Parameter Values
| Parameter | Description |
|---|---|
| set1 | Required. The set to insert into the current set. |
| set2 | Optional. More sets to insert into the current set. You can insert as many sets you like. Separate the sets with |
(pipe operator).See examples below. |
More Examples
Example
Use |= as a shortcut instead of
update():
y = {"google", "microsoft", "apple"}
x |= y
print(x)
Example
Insert more than one set:
y = {"google", "microsoft", "apple"}
z = {"cherry", "micra", "bluebird"}
x.update(y, z)
print(x)
Example
Join more than one set with the |= operator:
y = {"google", "microsoft", "apple"}
z = {"cherry", "micra", "bluebird"}
x |= y | z
print(x)
Related Pages
Tutorial: Python Sets
Method: Add Set Items
Method: Remove Set Items
Related Pages
Tutorial: Python Sets
Tutorial: Join Python Sets