Python Set symmetric_difference_update() Method (with Examples)

Python Set symmetric_difference_update() Method: In this tutorial, we will learn about the symmetric_difference_update() method of the set class with its usage, syntax, parameters, return type, and examples. By IncludeHelp Last updated : June 14, 2023

Python Set symmetric_difference_update() Method

The symmetric_difference_update() is an inbuilt method of the set class that is used to get the list of all elements which are not common in both sets and update this set (set1) with these elements i.e., it updates this set (set1) by removing the elements which are common in both sets.

Syntax

The following is the syntax of symmetric_difference_update() method:

set1.symmetric_difference_update(set2)

Parameter(s):

The following are the parameter(s):

  • set2 – It represents another set to be compared with this set (set1).

Return Value

The return type of this method is <class 'NoneType'>, it updates this set (set1) by removing the elements which are common in both sets.

Example 1: Use of Set symmetric_difference_update() Method

# declaring the sets
cars_1 = {"Porsche", "Audi", "Lexus"}
cars_2 = {"Porsche", "Mazda", "Lincoln"}

# printing sets before symmetric_difference_update()
print("Before calling symmetric_difference_update()...")
print("cars_1:", cars_1)
print("cars_2:", cars_2)

# symmetric_difference_update() method call
cars_1.symmetric_difference_update(cars_2)

# printing sets after symmetric_difference_update()
print("After calling symmetric_difference_update()...")
print("cars_1:", cars_1)
print("cars_2:", cars_2)

Output

Before calling symmetric_difference_update()...
cars_1: {'Audi', 'Lexus', 'Porsche'}
cars_2: {'Porsche', 'Mazda', 'Lincoln'}
After calling symmetric_difference_update()...
cars_1: {'Mazda', 'Audi', 'Lincoln', 'Lexus'}
cars_2: {'Porsche', 'Mazda', 'Lincoln'}

Example 2: Use of Set symmetric_difference_update() Method

# declaring the sets
x = {"ABC", "PQR", "XYZ"}
y = {"ABC", "PQR", "XYZ"}
z = {"DEF", "MNO", "ABC"}

# printing the results
print("Before calling symmetric_difference_update()...")
print("x:", x)
print("y:", y)
print("z:", z)

# printing the result of 
# symmetric_difference_update()
x.symmetric_difference_update(y)
y.symmetric_difference_update(z)
z.symmetric_difference_update(x)

# printing the results
print("After calling symmetric_difference_update()...")
print("x:", x)
print("y:", y)
print("z:", z)

Output

Before calling symmetric_difference_update()...
x: {'ABC', 'XYZ', 'PQR'}
y: {'ABC', 'XYZ', 'PQR'}
z: {'ABC', 'MNO', 'DEF'}
After calling symmetric_difference_update()...
x: set()y: {'XYZ', 'MNO', 'DEF', 'PQR'}
z: {'ABC', 'MNO', 'DEF'}



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.