Python Set symmetric_difference_update() Method Tutorial

In this section, we will learn what the Set symmetric_difference_update() method is and how to use it in Python.

What is Set symmetric_difference_update() Method in Python?

The Python Set symmetric_difference_update() method takes a reference to a set object and compares its elements with the Set object that invoked this method (AKA the original Set). After that, it will update the original Set object with two things:

– First, it will remove those elements of the original Set that are also in the reference Set.

– Second, it will copy those elements in the reference Set object that are not in the original Set and bring them to this original Set.

There’s another method called symmetric_difference() that does the same task. The difference, however, is that the symmetric_difference() method returns a new Set object instead of updating the original Set.

Python Set symmetric_difference_update() Method Syntax:

set.symmetric_difference_update(SetReference)

Set symmetric_difference_update() Method Parameter:

The method takes one argument and that is a reference to a Set object.

Set symmetric_difference_update() method Return Value

The method does not return a value.

Example: using python set symmetric_difference_update() method

set1 = {1,2,3,4,5,6,7,8,9,10}

set2 = {1,2,3,4,5,500,600,700}

set1.symmetric_difference_update(set2)

print(set1)

Output:

{6, 7, 8, 9, 10, 500, 600, 700}
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies