Home » Python

Python Set symmetric_difference_update() Method with Example

Python Set symmetric_difference_update() Method: Here, we are going to learn how to update this set by removing the elements which are common in both sets?
Submitted by IncludeHelp, on November 30, 2019

Set symmetric_difference_update() Method

symmetric_difference_update() method is used to get the list of all elements which are not common in both sets and update this set (set1) with these elements or we can say this method is used to update this set (set1) by removing the elements which are common in both sets.

Syntax:

    set1.symmetric_difference_update(set2)

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:

# Python Set symmetric_difference_update() 
# Method with Example

# 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:

# Python Set symmetric_difference_update() 
##Method with Example

# 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

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.