In the last tutorial, we discussed Set difference() method, which returns the difference between two given Sets. In this guide, we will see difference_update() method which doesn’t return anything but it updates the first Set with the Set difference. For example, calling method like this A.difference_update(B) would update the Set A with the A-B (elements that are in Set A but not in Set B).
Python Set difference_update() Syntax
X.difference_update(Y)
This would update the Set X with the X-Y
X-Y: Elements that are only in Set X and not in Set Y
Parameters: This method accepts a Set as a parameter.
Return Value: None. This method doesn’t return anything.
Python Set difference_update() Example
In the following example we have two Sets X and Y. We are calling X.difference_update(Y) which updates the Set X with the Set difference X – Y.
# Set X X = {"hello", 9, 10, "hi"} # Set Y Y = {9, "hi", 6, "BeginnersBook"} # calling difference_update() method X.difference_update(Y) # Displaying Set X and Y print("X is:", X) print("Y is:", Y)
Output:
Prakhar says
What is the difference between X.difference_update(Y) and X.difference(Y).