Easy Tutorial
❮ Python Func Int Python Os Ttyname ❯

Python Set intersection_update() Method

Python Sets


Description

The intersection_update() method is used to get the overlapping elements in two or more sets, i.e., to compute the intersection.

The intersection_update() method differs from the intersection() method because the intersection() method returns a new set, whereas the intersection_update() method removes the non-overlapping elements from the original set.

Syntax

The syntax for the intersection_update() method is:

set.intersection_update(set1, set2, ... etc)

Parameters

Return Value

None.

Example

Remove elements from set x that are not in set y:

Example 1

x = {"apple", "banana", "cherry"}  # y does not contain banana and cherry, they are removed
y = {"google", "tutorialpro", "apple"}

x.intersection_update(y)

print(x)

Output:

{'apple'}

Compute the intersection of multiple sets:

Example 2

x = {"a", "b", "c"}
y = {"c", "d", "e"}
z = {"f", "g", "c"}

x.intersection_update(y, z)

print(x)

Output:

{'c'}

Python Sets

❮ Python Func Int Python Os Ttyname ❯