The 'difference_update( )' Function is used to find the difference between two Sets. Let us understand it with the below example.
Say for example, we have a Set that contains three names, 'Mohan', 'Kriti' and 'Salim'.
And we have a second 'Set' that contains three names 'Sia', 'Andrew' and 'Kriti'.
x = {"Mohan", "Kriti", "Salim"} y = {"Sia", "Andrew", "Kriti"} x.difference_update(y) print(x)
So, in the above code we have created a 'Set' and initialised to the variable 'x'.
Then what we have done is, put the names, 'Sia', 'Andrew' and 'Kriti' in another set 'y'.
Then we have used the 'difference_update( )' Function.
The 'difference_update( )' Function will only take the values of x i.e. 'Mohan', 'Kriti' and 'Salim'.Then goto the set 'y' and try finding the common element in both the Sets.
And since, 'Kriti' is present in both the Sets, 'Kriti' will be discarded.
And just, 'Mohan' and 'Salim' would be updated value of the Set 'x'.
And we get the below output,