Python Set – intersection_update() Method

It is a method in python that modifies the main set with the common elements between the two sets. It modifies the given set. It can take multiple parameters in the argument.

Syntax:
set1.intersection_update(set2, set3, ….)

Also Read: Python Set – intersection() Method

Example 1:

# Python code to understand intersection_update() method in set
# www.codewindow.in

s1 = {2, 5, 8, 7, 9}
s = {10, 5, 2}

#intersection of s and s1
s.intersection_update(s1)
print(s)

#intersection of s1 and s
s1.intersection_update(s)
print(s1)

Output:

{2, 5}
{2, 5}

Explanation:
Here, it modified the original set and returned all the common elements of the main set along with the elements of the specified set.

Example 2:

# Python code to understand intersection_update() method in set
# www.codewindow.in

s1 = {"code", "window"}
s = {"code", "website"}
s2 = {"tech", "code"}

#union of s and s1?
s1.intersection(s, s2)
print(s1)

Output:

{'window', 'code'}

Explanation:
Here, it returned all the element of the main set along with the elements of the specified set (s) and set (s2) which is “code”, “window”.


Follow Us

You Missed

Also Checkout