Python Set update

Python Set x.update(y) method inserts elements, from set y into set x, that are not present in set x.

In this tutorial, we will learn the syntax of set.update() method and go through examples covering different scenarios for the arguments that we pass to update() method.

Syntax

The syntax of set.update() method is

set.update(other)

where

Parameter Description
other A set.

Examples

1 Update set x with elements of set y

In the following program, we will take two sets: x, y; and update set x with elements of set y.

Python Program

x = {'apple', 'banana', 'cherry'}
y = {'cherry', 'kiwi'}

x.update(y)

print(x)

Program Output

{'kiwi', 'apple', 'banana', 'cherry'}

Conclusion

In this Python Tutorial, we learned about Python set method update(). We have gone through the syntax of udpate() method, and its usage with example programs.