intersection_update() method in Python set
Update the main set with common elements only.
Using intersection_update() method
A={'a','b','c'}
B={'a','y','z'}
A.intersection_update(B)
print(A)
Output
{'a'}
Using more than one sets
We can use any number of sets with intersection_update()
A={'a','b','c'}
B={'a','y','z'}
C={'a','k','l'}
A.intersection_update(B,C)
print(A)
Output
{'a'}
intersection_update() method
Using string ( iterable object ) with intersection_update() method
A={'a','b','c','x','y'}
B='Alex'
A.intersection_update(B)
print(A)
Output
{'x'}
Using list with intersection_update() method.
A={'a','b','c'}
B=['a','x','y']
A.intersection_update(B)
print(A)
Output
{'a'}
« All set methods « Questions with solutions on set
intersection()
union()
difference()
symmetric_difference()
← Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
https://www.plus2net.com
plus2net.com