Easy Tutorial
❮ Ref Math Cos Python Intro ❯

Python Set symmetric_difference() Method

Python Sets


Description

The symmetric_difference() method returns a set of elements which are in either of the sets but not in their intersection, i.e., it removes elements that are present in both sets.

Syntax

The syntax for the symmetric_difference() method is:

set.symmetric_difference(set)

Parameters

Return Value

Returns a new set.

Example

Returns a new set composed of elements from both sets, but removes the elements that are duplicates:

Example 1

x = {"apple", "banana", "cherry"}
y = {"google", "tutorialpro", "apple"}

z = x.symmetric_difference(y)

print(z)

Output:

{'google', 'cherry', 'banana', 'tutorialpro'}

Python Sets

❮ Ref Math Cos Python Intro ❯