Set of pythons | difference_update ()

Python Methods and Functions

If A and B are two sets. The set diff () method will receive (A — B) and return a new set. The set diff_update () method modifies an existing set. If executed (AB), then A is modified to (AB), and if (BA) is executed, then B is modified to (BA).

Syntax :

  A.difference_update (B)  for (A - B)  B.difference_update (A)  for (B - A) 

The function returns None and overrides the value of the existing set. 
In this example, we'll get the difference between the two sets and show how diff_update works.

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

 
Driver code

A = { 10 , 20 , 30 , 40 , 80 }

B = { 100 , 30 , 80 , 40 , 60 }

 
# Changes A and returns None
A.difference_update (B)

 
# Prints the modified set

print (A )

Output:

 {20 , 10} 




Tutorials