Python Set symmetric_difference()

Python Set x.symmetric_difference(y) method returns a new set with elements from both x and y, but not the elements that are common to x and y. In other words, then method returns (x.union(y) – x.intersection(y)).

In this tutorial, we will learn the syntax of set.symmetric_difference() method and go through examples covering different scenarios for the arguments that we pass to symmetric_difference() method.

Syntax

The syntax of set.symmetric_difference() method is

</>
Copy
set.symmetric_difference(other)

where

ParameterDescription
otherA set.

Examples

1. Symmetric difference of x, y

In the following program, we will take two sets: x, y; and find their symmetric difference.

Python Program

</>
Copy
x = {'apple', 'banana', 'cherry'}
y = {'cherry', 'kiwi'}

result = x.symmetric_difference(y)

print(result)

Program Output

{'kiwi', 'banana', 'apple'}

Conclusion

In this Python Tutorial, we learned about Python set method symmetric_difference(). We have gone through the syntax of symmetric_difference() method, and its usage with example programs.