Python Set intersection_update() Method
Description
intersection_update() The method gets the elements that overlap in two or more sets, that is, it computes the intersection.
intersection_update() The method differs from intersection() method, because the intersection() The method returns a new set, whereas intersection_update() The method removes non-overlapping elements from the original set.
Syntax
Syntax of the intersection_update() method:
set.intersection_update(set1, set2 ... etc)
Parameters
- set1 -- required, the set in which to find common elements
- set2 -- optional, other sets in which to find common elements; multiple sets are allowed, separated by commas
Return Value
none.
Example
Remove elements from set x that are not in set y:
Example 1
x
= {
"
apple
"
,
"
banana
"
,
"
cherry
"
}
Set y does not contain banana and cherry, so they are removed
y
= {
"
"
,
"
runoob
"
,
"
apple
"
}
x
.
intersection_update
(
y
)
(
x
)
The output is:
{'apple'}
Compute the intersection of multiple sets:
Example 1
x
= {
"
a
"
,
"
b
"
,
"
c
"
}
y
= {
"
c
"
,
"
d
"
,
"
e
"
}
z
= {
"
f
"
,
"
g
"
,
"
c
"
}
x
.
intersection_update
(
y
,
z
)
(
x
)
The output is:
{'c'}
Python Sets