Python Set intersection_update()

The intersection_update() finds the intersection of different sets and updates it to the set that calls the method.

Example

A = {1, 2, 3, 4}
B = {2, 3, 4, 5}

# updates set A with the items common to both sets A and B A.intersection_update(B)
print('A =', A) # Output: A = {2, 3, 4}

intersection_update() Syntax

The syntax of the intersection_update() method is:

A.intersection_update(*sets)

Here, *sets indicates that set A can be intersected with one or more sets.


intersection_update() Parameter

The intersection_update() method allows an random number of arguments:

  • *sets - denotes that the methods can take one or more arguments

For example,

A.intersection_updata(B, C)

Here, the method has two arguments, B and C.


intersection_update() Return Value

The intersection_update() method doesn't return any value.


Example: Python Set intersection_update()

A = {1, 2, 3, 4}
B = {2, 3, 4, 5, 6}
C = {4, 5, 6, 9, 10}

# performs intersection between A, B and C and updates the result to set A A.intersection_update(B, C)
print('A =', A) print('B =', B) print('C =', C)

Output

A = {4}
B = {2, 3, 4, 5, 6}
C = {4, 5, 6, 9, 10}

In the above example, we have used intersection_update() to compute the intersection between sets A, B and C. The result of intersection is updated to set A.

That's why we have A = {4} as the output because 4 is the only item that is present in all three sets. Whereas, sets B and C are unchanged.


Also Read:

Did you find this article helpful?