











Python Set difference_update()
Difference_update() in Python Sets
This method helps to find out the difference between two sets.
The difference_update() method is same as the difference() method in Python sets. The only difference is that difference() method returns a new set whereas difference_update() method updates the calling set.


Let us understand this with an example:
- set A = { 1, 2, 3, 4, 5 }
- set B = { 1, 3, 5, 7 }
For A – B
- A.difference(B) : It will return a new set having i.e. newSet = { 2, 4 }
- A.difference_update(B) : It will update the set A with A-B i.e. set A = { 2, 4 }
For B – A
- B.difference(A) : It will return a new set having i.e. newSet = { 7 }
- B.difference_update(A) : It will update the set B with B-A i.e. set B = { 7 }
Syntax :
set1.difference_update(set2)
Return Type :
- difference_update() method does not return any value. So it returns None.
- It only changes the values of existing set.
#difference of sets using difference_update() method
# A – B will be updates in set A
A = { 1, 2, 3, 4, 5, 6 }
B = { 2, 4, 6, 8, 10 }
print( A.difference_update(B) ) #returns NONE
print(A) # set A is updated
print(B) # set B remained same
# B – A will be updates in set B
A = { 1, 2, 3, 4, 5, 6 }
B = { 2, 4, 6, 8, 10 }
print( B.difference_update(A) ) #returns NONE
print(A) # set A remained same
print(B) # set B is updated
Output : None {1, 3, 5} {2, 4, 6, 8, 10} None {1, 2, 3, 4, 5, 6} {8, 10}
Login/Signup to comment