Tutorial Study Image

Python intersection_update()

The intersection_update() function in python helps in set update. It first finds out the intersection of given sets. And update the first set with result elements of set intersection. The set intersection gives a new set containing the elements common in all given sets.


A.intersection_update(*other_sets) #where A is a set of any iterables, like strings, lists, and dictionaries
 

intersection_update() Parameters:

The intersection_update() function can take many set parameters for the comparison(* indicates) and separate the sets with commas.

 

Parameter Description Required / Optional
A The set to search for equal items in and update Required
other_sets The other set to search for equal items Optional

intersection_update() Return Value

This method doesn't return any values. It updates the original set.

result = A.intersection_update(B, C) consider this, here result will be none. A will be the intersection of A, B, C. And B & C remains unchanged.

Examples of intersection_update() method in Python

Example 1: How intersection_update() works in Python?


A = {'a', 'b', 'c', 'd'}
B = {'c', 'e', 'f', 'a'}
result = A.intersection_update(B)
print('result =', result)
print('A =', A)
print('B =', B)
 

Output:


result = None
A = {'a', 'c'}
B = {'c', 'e', 'f', 'a'}

Example 2: Python method intersection_update() with two parameters


A = {'a', 'b', 'c', 'd'}
B = {'c', 'e', 'f', 'a'}
C = {'d', 'f', 'a', 'g'}
result = C.intersection_update(B, A)

print('result =', result)
print('C =', C)
print('B =', B)
print('A =', A)
 

Output:


result = None
C = {'a'}
B = {'c', 'e', 'f', 'a'}
A = {'a', 'b', 'c', 'd'}