Python Set symmetric_difference_update()


Python Set symmetric_difference_update() method

Python Set symmetric_difference_update() method updates the original set with the symmetric difference of the set and the given iterable.

Please refer Set symmetric_difference() method to see how to find the symmetric difference of given 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_update() method, with examples.

Syntax of Set symmetric_difference_update()

The syntax to call symmetric_difference_update() method is

set.symmetric_difference_update(s)

Parameters

Set symmetric_difference_update() method takes one parameter.

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

Return value

symmetric_difference_update() method returns None.

Usage

Update set with symmetric difference of two sets

The following statement shows how to do the symmetric difference update of a set set_1 with set_2 using set.symmetric_difference_update() method.

set_1.symmetric_difference_update(set_2)

This statement is equivalent to either of the following.

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

Symmetric difference update of a set with a list

The following statement shows how to load set_1 with the symmetric difference of set_1 and a list list_1 using set.symmetric_difference_update() method.

set_1.symmetric_difference_update(list_1)

Examples

1. Symmetric difference update 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 update with these two sets.

Call symmetric_difference_update() method on set_1 object, and pass set_2 object as argument. Also, we shall print set_1 before and after the update.

Python Program

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

print(f"set_1 : {set_1}")

set_1.symmetric_difference_update(set_2)

print(f"set_1 after update : {set_1}")

Output

set_1 : {'d', 'b', 'c', 'a'}
set_1 after update : {'m', 'c'}

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 and update the set_1 with the result.

Call symmetric_difference_update() method on set_1 object, and pass list_1 object as argument.

Python Program

set_1 = {'a', 'b', 'c', 'd'}
list_1 = ['a', 'm', 'k']

print(f"set_1 : {set_1}")

set_1.symmetric_difference_update(list_1)

print(f"set_1 after update : {set_1}")

Output

set_1 : {'a', 'c', 'b', 'd'}
set_1 after update : {'b', 'c', 'k', 'm', 'd'}

Summary

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