Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Set difference() Method

Python Set difference() Method

Python3 Lists Python Sets


Description

The difference() method returns the difference of sets, that is, the returned set contains elements that are in the first set but not in the second set (the method's argument).

Syntax

Syntax of the difference() method:


set.difference(set)

Parameters

Return Value

Returns a new set.

Example

Return a set whose elements 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 is:


{'cherry', 'banana'}

Python3 Lists Python Sets