Python Set difference() Method

Python3 列表Python Set


Description

The difference() method returns the difference between sets, i.e., the returned set contains elements that are in the first set but not in the second set (the method's parameter).

Syntax

The syntax of the difference() method is:

set.difference(set)

Parameters

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

Return Value

Returns a new set.

Examples

Return a set containing elements that are in set x but not in set y:

Example 1

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

The output result is:

{'cherry', 'banana'}

Python3 列表Python Set

Other Extensions