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

Python Set symmetric_difference() Method

Python Set symmetric_difference() Method

Python3 Lists Python Sets


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

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

= {

"

google

"

,

"

runoob

"

,

"

apple

"

}

z

=

x

.

symmetric_difference

(

y

)

print

(

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

print

(

result

)

 

Output: {1, 2, 5, 6}

The output is:



{1, 2, 5, 6}

Python3 Lists Python Sets