Python Set difference() Method
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
- set -- required, the set used to compute the difference
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
= {
"
"
,
"
microsoft
"
,
"
apple
"
}
z
=
x
.
difference
(
y
)
(
z
)
The output is:
{'cherry', 'banana'}
Python Sets