Skip to main content

intersection_update()

The intersection_update() function in Python is a set method that updates the set calling the method with the intersection of itself and another set passed as an argument. It modifies the original set in place by removing elements that are not present in both sets.

Parameter Values

Parameter Description
others

A set or any iterable object whose elements will be used to update the set.

Return Values

The intersection_update() method in Python returns None.

How to Use intersection_update() in Python

Example 1:

The intersection_update() method updates the set by keeping only the elements that are present in both sets.

set1 = {1, 2, 3, 4}
set2 = {2, 3, 4, 5}
set1.intersection_update(set2)
print(set1) # Output: {2, 3, 4}
Example 2:

If there are duplicate elements, intersection_update() will only keep one copy in the updated set.

set1 = {1, 2, 2, 3}
set2 = {2, 3, 3, 4}
set1.intersection_update(set2)
print(set1) # Output: {2, 3}
Example 3:

The intersection_update() method can be used with multiple sets to update the original set to contain only elements present in all specified sets.

set1 = {1, 2, 3, 4}
set2 = {2, 3, 4, 5}
set3 = {3, 4, 5, 6}
set1.intersection_update(set2, set3)
print(set1) # Output: {3, 4}