In this Python guide, we will go through the set itersection_update() method with the help of examples. Python set intersection_update() method is used to remove all the items that not present in the sets.
Before going through this article, you should have knowledge of Python set.
Python Set intersection_update() Method
set intersection_update() method is a set method that is used to remove all the items that are present in both sets ( or all the set passed into intersection_update() method.)
Syntax
The syntax of set intersection_update() method is:-
set.intersection_update(set1, set2,.....etc)
Parameter
intersection_update function in Python takes multiple parameters.
- set1 – Required. A set to search for equal items.
- set2 – Optional. A set to search for equal items.
Return Value
set intersection_update() method does not return new set it update the original set.
Set intersection_update() example
Example 1:
set1 = {'Python', 'Java', 'JavaScript', 'C++'}
set2 = {'HTML', 'CSS', 'Python', 'C++'}
set1.intersection_update(set2)
print(set1)
Output will be:- {‘C++’, ‘Python’}
Example 2:
my_set1 = {'Python', 'Java', 'JavaScript', 'C++'}
my_set2 = {'HTML', 'CSS', 'Python', 'C++'}
my_set3 = {'Ruby','C++','Python', 'Lua'}
result = my_set1.intersection(my_set2, my_set3)
print(result)
Output will be:- {‘C++’, ‘Python’}
Conclusion
Here we have learned all about the intersection_update function in python. Python set intersection() method returns a new set that contains only items that exist in both sets. intersection function in Python accepts more than one parameters
I how this is very useful for you. To learn Python from scratch to advance, this place will best for you.
Other set methods
For More Information:- Click Here