In this Python tutorial, we’ll learn how to properly make use of Python set difference update method. We’ll go through multiple code examples for detailed demonstration of how difference_update() method works.
Introduction: Python Set Difference Update Method
In our previous tutorial, we’ve discussed how Python set difference method returns a new set which consists of items of original set that are not present in specified set.
Now the difference update method apply the same strategy but instead of creating a new set, it just updates the original set with the resulting values.
Don’t worry, we’ll understand it in detail with the help of practical Python code examples. But first, let’s walk through its syntax.
Syntax of difference_update() Method
setA.difference_update(setB)
- Python set difference update method takes one argument and its a Python set.
- Here set A is the original set and set B is the specified set.
- The difference_update() method will only take the items of original set(set A in this case) that’re not present in set B.
- This method does not return anything. It just updates the original set(set A in this case) with the resulting set values.
Implementing Python Set Difference Update Method (Easy Examples)
Below are some examples that will make us properly understand the working of difference_update() method of Python set.
Example 1: Applying difference_update() method on Two Sets
sA={'n','r',66,98,'Awais'} sB={223,65,(3,5),66,98,(4,5)} sA.difference_update(sB) print('Set A is: ',sA)
Output
{'n', 'Awais', 'r'}
- First, we’ve created two Python sets.
- Secondly, we’ve applied Python set difference update method on them where sA is the original set and sB is the specified set.
- Finally, we’ve printed the value of original set (sA in this case) and we can see that the original set is updated and now consists of only its unique items that are not present in sB but present in sA.
Let’s now set sB to original and sA to specified. See below code:
sB.difference_update(sA) print('Set B is: ',sB)
Output
Set B is: {65, (4, 5), (3, 5), 223}
We can see that only the unique items of sB are included in the resulting original set(sB in this case).
Example 2: When No Unique Items Are Found In Original Set
setA={'n','r',66,98,'Awais'} setB={'n','r',66,98,'Awais'} setA.difference_update(setB) print('Set A is now: ',setA)
Output
Set A is now: set()
We can see that when there are no unique items found in original set (setA in this case) then the original set shows set() which denotes an empty set.
Example 3: Python Set Difference Update Method Return Value
setA={1,2,3,5,6} setB={5,8,9,7} retVal=setA.difference_update(setB) print('The returned Value is: ',retVal)
Output
The returned Value is: None
We can see that the difference update method does not return anything. It just updates the original set(set A in this case) with the resulting unique items. If no unique items are found then the original set shows set() which denotes an empty Python set.
Example 4: Applying difference_update() on List of Sets Using Python For Loop
lstSetA=[{2,3,8},{'b','g','k'},{(2,3,9,7,4),99},{'abc','def'}] lstSetB=[{2,8,9,87},{'jgb','g','k',9834},{(2,7,4),99},{'a','def',56}] for v1,v2 in zip(lstSetA,lstSetB): v1.difference_update(v2) print(lstSetA)
Output
[{3}, {'b'}, {(2, 3, 9, 7, 4)}, {'abc'}]
- We’ve created two Python lists.
- After that, we’ve used Python for loop which have two variables. First will store items of first set(lstSetA) and second variable will store items of second set(lstSetB). Its order sensitive which means that in the case of (lstSetB,lstSetA), first variable will store the items of lstSetB while the second variable will store the items of of lstSetA.
- Using the values of list items(these items are sets in this case), we’ve applied difference_update method on them.
- Finally, we’ve displayed the values of original set(lstSetA in this case).
You can try it by making the lstSetB original and lstSetA specified like shown below:
for v1,v2 in zip(lstSetB,lstSetA): v1.difference_update(v2) print(lstSetB)
Output
[{87, 9}, {'jgb', 9834}, {(2, 7, 4)}, {'a', 56}]
So this is how we can properly use Python set difference update method. Feel free to ask if you still have any questions regarding its usage. We’ll be super glad to answer all your questions.
Also, if you have learned something new from this post then don’t hesitate to share it with your squad of Python programmers.
Conclusion
In conclusion, now we’ve a detailed practical knowledge of Python set difference update method. Don’t forget to leave your valuable feedback on this post.
You can discover more concepts about Python programming in this site. Thank you for reading this article.