In this Python tutorial, we’ll learn about the proper usage of Python set intersection update method. We’ll jump into multiple Python code examples in order to better understanding.
Introduction: Python Set Intersection Update Method
In our previous article, we’ve discussed about the intersection method that returns a new Python set which consists of common values of all sets. Click here to learn about it in detail.
The intersection update method takes the common items of all specified sets and updates the set calling that function with those values. So this set will contain only those common items.
Let’s now understand working of this method with the help of detailed Python code examples. But first, let’s walk through its syntax.
Syntax of intersection_update() Method
setA.intersection_update(one or more sets)
- The intersection_update() method can take one or more Python sets as an argument.
- This method does not return anything. It just updates the set that calls it with the resulting common items.
- If no common items are found then it updates its calling set with set(), which denotes an empty Python set.
Implementation of Python Set Intersection Update Method (Multiple Examples)
Follow below examples in order to understand the usage of intersection update method in detail.
Example 1: Apply intersection_update() on Two Python Sets
a = {1,2,3,9,8} b = {3,6,5,2,99} a.intersection_update(b) print('Value of set A is now: ', a) print('Value of set B is now: ' ,b)
Output
Value of set A is now: {2, 3} Value of set B is now: {2, 99, 3, 5, 6}
- In this program, first we’ve created two simple Python sets.
- After that, we’ve applied intersection_update() method on them in which set(a) is calling this method and set(b) is passed as an argument.
- Finally, we’ve printed both these sets and we can see that the items of set(set a in this case) that called the method now contains only the common items of both sets.
Let’s now call this method using set(b) and pass set(a) as an argument to it. See below:
b.intersection_update(a) print('Set A is now: ',a) print('Set B is now: ',b)
Output
Set A is now: {1, 2, 3, 8, 9} Set B is now: {2, 3}
We can see that set(b) is now updated with common items of both sets while set(a) remains the same.
Example 2: intersection_update() on Multiple Python Sets
a = {1,2,3} b = {2,3,5,6} c = {3,9,8,6} a.intersection_update(b,c) print('Set A: ', a) print('Set B: ', b) print('Set C: ', c)
Output
Set A: {3} Set B: {2, 3, 5, 6} Set C: {8, 9, 3, 6}
- In this program, we’ve created 3 simple Python sets.
- After that, we’ve called the intersection_update() method of set(a) and have passed set b and c as an argument to it.
- Finally, we’ve displayed the values of all these sets. As a result, we can see that only set(a) has been updated with the common values of all sets. While the other sets remains the same.
Do try it by calling this method from set b or c and pass the other sets as an argument to it.
Example 3: Calling Set Replaced With An Empty Set
The calling set(set whose intersection_update() method is used) is set to an empty set(set()) when no common items are found. See below code:
x={7,8,9} y={4,5,6} x.intersection_update(y) print('Set X: ',x)
Output
Set X: set()
We can see that the calling set is replaced with set() when no common items are found. The set() denotes an empty set.
Example 4: intersection update Method with no Arguments
x={1,2,3} x.intersection_update() print(x)
Output
{1, 2, 3}
We can see that if we don’t pass any argument to our Python set intersection update method then it does not update its calling method. In the output block, we can see that the set(x) shows the same values.
Example 5: Return Value of Python Set Intersection Update Method
l = {'x','y','z'} m = {'l','x','z'} returnVal = l.intersection_update(m) print('Return value is:', returnVal)
Output
Return value is: None
We can see that this method does not return anything. It just updates its calling set with the common items(if they exist). Or else it updates its calling method with set() which denotes an empty set of Python.
Example 6: intersection_update() on Lists of Sets using Python For Loop
x=[{'x','y','z'},{4,5,6},{(3,4),2,3}] y=[{'b','y','r'},{55,5,62},{2.4,7.6}] z=[{'m','n','y'},{2,4,5},{1,2,3}] for v1,v2,v3 in zip(x,y,z): v1.intersection_update(v2,v3) print(x)
Output
[{'y'}, {5}, set()]
- First, we’ve created three Python lists which consists of multiple items(sets in this case).
- Secondly, we’ve applied Python for loop on them which will take each item of these Python lists in each iteration. Click here to learn about iterating over multiple Python lists simultaneously.
- The intersection_update() method will be called by the items of list(x). These items are sets. While the items of other lists will be passed as an arguments to this method.
- After the execution of for loop is completed, we’ve displayed the values of list(x) and it shows an updated version. Reason is that we’ve used its items(sets in this case) to call the intersection_update method.
We hope you’ve learned something new from this article. If so, then do share it with other Python developers.
Click here to learn more about Python set methods.
Do ask if you still have any queries regarding Python set intersection update method. We’ll be delighted to solve all your queries.
Conclusion
In conclusion, we hope you now have a detailed practical understanding of how Python set intersection update method works. Do give this method a try and don’t forget to share your valuable feedback with us.
You can discover more valuable articles on Python programming in this site. Thank you for reading this article.