The difference_update() method helps in an in-place way of differentiating the . 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 modfied into (B – A).


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.

#  code to get the difference between two sets
# using difference_update() between set A and set B

# Driver Code
A = {, 20, 30, 40, 80}
B = {0, 30, 80, 40, 60}

# Modifies A and returns None

# Prints the modified set
print (A)


{20, 10}

If you like and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Please write to us at to report any issue with the above content.

Source link


Please enter your comment!
Please enter your name here