The Set intersection_update() method finds the common elements between two or more sets. It modifies the given set by updating it to contain only the elements that are present in both the original set and the second set (or all sets if the comparison is done between more than two sets).
Syntax
my_set.intersection_update(set1, set2, ...)intersection_update() Parameters
Python Set intersection_update() method parameters:
| Parameter | Condition | Description |
|---|---|---|
set1, set2, ... |
Optional | A comma-separated list of one or more sets to find common items in |
intersection_update() Return Value
Python Set intersection_update() method does not return any value: it modifies the original set in-place.
Examples
Example 1: Intersection between Two Sets
A = {'Tom', 'Anna', 'David'}
B = {'David', 'Tom'}
A.intersection_update(B)
print(A) # Output: {'David', 'Tom'}output
{'David', 'Tom'}Example 2: Intersection between Multiple Sets
The intersection_update() method allows to compute the common elements between multiple sets:
A = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
B = {3, 4, 5, 6, 7}
C = {6, 7, 8, 9, 10}
A.intersection_update(B, C)
print(A) # Output: {6, 7}output
{6, 7}Example 3: Intersection between a Set and a List
You can also pass iterables like list: intersection_update() first converts the iterables to sets and then computes the intersection in-place.
For example:
A = {'a', 'e', 'i', 'o', 'u'}
B = ['d', 'e', 'f']
A.intersection_update(B)
print(A) # Output: {'e'}output
{'e'}Example 4: Intersection between a Set and a Tuple
You can also pass iterables like tuple: intersection_update() first converts the iterables to sets and then computes the intersection in-place.
For example:
A = {'a', 'e', 'i', 'o', 'u'}
B = ('d', 'e', 'f')
A.intersection_update(B)
print(A) # Output: {'e'}output
{'e'}Example 5: Intersection between a Set and a Dictionary
You can also pass iterables like dictionary: intersection_update() first converts the iterables to sets and then computes the intersection in-place.
For example:
A1 = {'a', 'e', 'i', 'o', 'u'}
A2 = {'a', 'e', 'i', 'o', 'u'}
B = {1: 'd', 2: 'e', 3: 'f'}
C = {'d': 1, 'e': 2, 'f': 3}
A1.intersection_update(B)
A2.intersection_update(C)
print(A1) # Output: set()
print(A2) # Output: {'e'}output
set()
{'e'}Example 6: Intersection between a Set and a String
You can also pass iterables like string: intersection_update() first converts the iterables to sets and then computes the intersection in-place.
For example:
A = {'a', 'e', 'i', 'o', 'u'}
B = 'def'
A.intersection_update(B)
print(A) # Output: {'e'}output
{'e'}Equivalent Operator &= for intersection_update()
Set intersection can be performed with the &= operator as well, providing an alternative to the intersection_update() method.
For example:
A = {1, 2, 3, 4, 5}
B = {3, 4, 5, 6, 7}
A &= B
print(A) # Output: {3, 4, 5}output
{3, 4, 5}Of course, you can use the & operator with multiple sets:
A = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
B = {3, 4, 5, 6, 7}
C = {6, 7, 8, 9, 10}
A &= B & C
print(A) # Output: {6, 7}output
{6, 7}