Python Set symmetric_difference() Method
The symmetric_difference() method can be used to find the symmetric difference of two sets.
The symmetric_difference() method returns the set of non-repeating elements of the two sets, that is, elements present in both sets are removed.
Syntax
Syntax of the symmetric_difference() method:
set.symmetric_difference(set)
Parameters
- set -- a set
Return Value
Returns a new set.
Example
Returns a new set formed from the two sets, with duplicate elements removed:
Example 1
x
= {
"
apple
"
,
"
banana
"
,
"
cherry
"
}
y
= {
"
"
,
"
runoob
"
,
"
apple
"
}
z
=
x
.
symmetric_difference
(
y
)
(
z
)
The output is:
{'google', 'cherry', 'banana', 'runoob'}
You can also use the ^ operator to achieve the same effect:
Example
set1
=
{
1
,
2
,
3
,
4
}
set2
=
{
3
,
4
,
5
,
6
}
result
=
set1 ^ set2
(
result
)
Output: {1, 2, 5, 6}
The output is:
{1, 2, 5, 6}
Python Sets