Python Set intersection_update() Method Tutorial

In this section we will learn what the Set intersection_update() method is and how to use it in Python.

What is Set intersection_update() Method in Python?

The Python Set intersection_update() method is used to find the common elements between two Set objects and return those elements as part of a new Set object.

Basically, this method takes one argument and that is a reference to a Set object. Then this method will compare the elements of the Set object that this method is invoked with and the reference Set, and finally it will update the original Set object (the one that this method is invoked with) by removing those elements that are not common in the reference Set object.

Note: there’s another method called intersection () that does the same task. The difference however is that the intersection() method returns a new Set object but the intersection_update() method updates the original Set method (the one that invoked the method) by removing those elements that are not common in the reference Set object and returns no value.

Python Set intersection_update() Method Syntax:

set.intersection_update(referenceSet)

Set intersection_update() Method Parameter:

The method takes one argument and that is a reference to a Set object.

Set intersection_update() method Return Value

The method does not return a value.

Example: using python set intersection_update() method

set1 = {1,2,3,4,5,6,7,8,9,10}

set2 = {1,2,3,4,5,500,600,700}

set1.intersection_update(set2)

print(set1)

print(set2)

Output:

{1, 2, 3, 4, 5}

{1, 2, 3, 4, 5, 500, 600, 700}

Leave a Reply