Python Set symmetric_difference()

Python Set symmetric_difference() method

Python Set symmetric_difference() method returns the difference of union of the two sets and intersection of the sets.

The symmetric difference in terms of union and intersection is given below.

symmetric_difference = union - intersection

In this tutorial, you will learn the syntax and usage of Set symmetric_difference() method, with examples.

Syntax of Set symmetric_difference()

The syntax to call symmetric_difference() method is

set.symmetric_difference(s)

Parameters

Set symmetric_difference() method takes one parameter.

ParameterDescription
sRequired
An iterable, like a set, a list, a string, a tuple, etc.
Python Set symmetric_difference() method parameters table

Return value

symmetric_difference() method returns a new set object created from the symmetric difference operation of the original set and the given iterable.

Usage

Symmetric difference of two sets

The following statement shows how to find the symmetric difference of two sets set_1 and set_2 using set.symmetric_difference() method, and store the result in symm_diff_output.

symm_diff_output = set_1.symmetric_difference(set_2)

This statement is equivalent to the following.

symm_diff_output = set_1.union(set_2).difference(set_1.intersection(set_2))

Symmetric difference of a set and a list

The following statement shows how to find the symmetric difference of a set set_1 and a list list_1 using set.symmetric_difference() method, and store the result in symm_diff_output.

symm_diff_output = set_1.symmetric_difference(list_1)

Examples

1. Finding symmetric difference of two sets in Python

In this example, we shall take two sets in set_1 and set_2. We have to find the symmetric difference of these two sets.

Call symmetric_difference() method on set_1 object, and pass set_2 object as argument. Store the returned value in symm_diff_output.

Python Program

# Take two sets
set_1 = {'a', 'b', 'c', 'd'}
set_2 = {'a', 'b', 'd', 'm'}

# Find the symmetric difference of the two sets
symm_diff_output = set_1.symmetric_difference(set_2)

print(symm_diff_output)
Run Code Copy

Output

{'c', 'm'}

2. Finding symmetric difference of a set and a list in Python

In this example, we shall take a set in set_1 and a list in list_1. We have to find the symmetric difference of these two collections.

Call symmetric_difference() method on set_1 object, and pass list_1 object as argument. Store the returned value in symm_diff_output.

Python Program

# Take two sets
set_1 = {'a', 'b', 'c', 'd'}
list_1 = ['a', 'm', 'k']

# Find the symmetric difference of the set and list
symm_diff_output = set_1.symmetric_difference(list_1)

print(symm_diff_output)
Run Code Copy

Output

{'d', 'k', 'b', 'm', 'c'}

Summary

In this Python Set Tutorial, we learned how to use Set symmetric_difference() method to find the symmetric difference of items in the set and given iterable in Python, with the help of well detailed examples.

Related Tutorials

Code copied to clipboard successfully 👍