Skip to main content

difference_update()

The difference_update() method in Python is a set method that removes all elements from the set that are also present in another specified set. It modifies the original set in place and does not return a new set. This method helps to find the difference between two sets by updating the set with elements only found in the original set and not in the specified set.

Parameter Values

Parameter Description
iterable

An iterable object like a list, set, tuple etc. whose elements need to be removed from the set.

Return Values

The difference_update() method returns None; it updates the set in place.

How to Use difference_update() in Python

Example 1:

The difference_update() method removes the items that exist in both sets while keeping only the items that are unique to the first set.

set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
set1.difference_update(set2)
print(set1) # Output: {1, 2}