Easy Tutorial
❮ Python Att Dictionary In Python Att Dictionary Copy ❯

Python Set difference() Method

Python Set


Description

The difference() method is used to return the difference of 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 for the difference() method is:

set.difference(set)

Parameters

Return Value

Returns a new set.

Example

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)

Output:

{'cherry', 'banana'}

Python Set

❮ Python Att Dictionary In Python Att Dictionary Copy ❯