Python Set difference_update() Method

The set.difference_update() method updates the set on which the method is called with the elements that are common in another set passed as an argument.

Syntax:

set.difference_update(another_set)

Parameters:

another_set: (Required.) The set with which the difference is to be found.

Return type:

No return value.

The following example demonstrates the set.difference_update() method.

Example: difference_update()
empids = {1,2,3,4,5}
mgrids = {1,2,3}
empids.difference_update(mgrids)
print("empids: ", empids)
print("mgrids: ", mgrids)
Output
empids:  {4, 5}
mgrids:  {1, 2, 3}

In the above example, empids.difference_update(mgrids) updates the empids set by removing command elements present in mgrids.

The following example demonstrates difference_update() method with the string elements.

Example: difference_update()
cities = {'Mumbai','Chicago','Honk Kong','New York'}
otherCities = {'Mumbai','Chicago'}

cities.difference_update(otherCities)

print("Updated cities: ", cities)
print("otherCities: ", otherCities)
Output
Updated cities:  {'Honk Kong', 'New York'}
otherCities:  {'Mumbai', 'Chicago'}