Thursday, October 2, 2025
HomeLanguagesPython Set | difference_update()

Python Set | difference_update()

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 Code
A = {10, 20, 30, 40, 80}
B = {100, 30, 80, 40, 60}
 
# Modifies A and returns None
A.difference_update(B)
 
# Prints the modified set
print (A)


Output: 
 

{20, 10}

 

RELATED ARTICLES

Most Popular

Dominic
32331 POSTS0 COMMENTS
Milvus
85 POSTS0 COMMENTS
Nango Kala
6703 POSTS0 COMMENTS
Nicole Veronica
11867 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11926 POSTS0 COMMENTS
Shaida Kate Naidoo
6818 POSTS0 COMMENTS
Ted Musemwa
7079 POSTS0 COMMENTS
Thapelo Manthata
6775 POSTS0 COMMENTS
Umr Jansen
6776 POSTS0 COMMENTS