Easy Tutorial
❮ Python Os Write Python String Rstrip ❯

Python Set difference_update() Method

Python Sets


Description

The difference_update() method is used to remove elements that are present in both sets.

The difference_update() method differs from the difference() method in that difference() returns a new set with the common elements removed, while difference_update() removes the elements directly from the original set without returning a value.

Syntax

The syntax for the difference_update() method is:

set.difference_update(set)

Parameters

Return Value

None.

Example

Removing elements that are present in both sets:

Example 1

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}

x.difference_update(y)

print(x)

Output:

{'cherry', 'banana'}

Python Sets

❮ Python Os Write Python String Rstrip ❯