The difference_update() method helps in an in-place way of differentiating the set. The previously discussed set difference() helps to find out the difference between two sets and returns a new set with the difference value, but the difference_update() updates the existing caller set.
If A and B are two sets. The set difference() method will get the (A – B) and will return a new set. The set difference_update() method modifies the existing set. If (A – B) is performed, then A gets modified into (A – B), and if (B – A) is performed, then B gets modified into (B – A).
Syntax:Â
Â
A.difference_update(B) for (A - B) B.difference_update(A) for (B - A)
Â
The function returns None and changes the value of the existing set.Â
In this example, we will get the difference between two sets and show how the difference_update works.
Â
Python3
# Python code to get the difference between two sets# using difference_update() between set A and set BÂ
# Driver CodeA = {10, 20, 30, 40, 80}B = {100, 30, 80, 40, 60}Â
# Modifies A and returns NoneA.difference_update(B)Â
# Prints the modified setprint (A) |
Output:Â
Â
{20, 10}
Â


… [Trackback]
[…] Here you will find 19510 more Information on that Topic: geeksforgeeks.org/python-set-difference-update/ […]
… [Trackback]
[…] Read More on to that Topic: geeksforgeeks.org/python-set-difference-update/ […]