Python Set difference_update() Method

Python3 列表Python Sets


Description

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

difference_update()The method anddifference()The difference between the methods is thatdifference()The method returns a new set with common elements removed, whiledifference_update()The method directly removes elements from the original set and has no return value.

Syntax

Syntax of the difference_update() method:

set.difference_update(set)

Parameters

  • set -- Required, the set used to compute the difference.

Return Value

None.

Examples

Remove the elements present in both sets:

Example 1

x = {"apple", "banana", "cherry"} y = {"google", "microsoft", "apple"} x.difference_update(y) print(x)

The output result is:

{'cherry', 'banana'}

Python3 列表Python Sets

Other Extensions