AppDividend
Latest Code Tutorials

Python Set symmetric_difference_update() Method Example

2

Python Set symmetric_difference_update() is an inbuilt method that works same like Python Set symmetric_difference() method. This means it finds the difference between two sets. But the only difference is, it updates the calling set with the result set.

Python Set symmetric_difference_update()

Python symmetric_difference_update() method updates the set calling the symmetric_difference_update() with the symmetric difference of sets. The symmetric difference of two sets is the set of elements that are in either of the sets but not in both.

Syntax

A.symmetric_difference_update(B)

Here, A is calling set and B is another set. This method finds the difference between these two sets and then it updates the value in set A.

Return Value

The symmetric_difference_update() method returns None. But it updates the value of calling set with the value of the difference between the two sets.

See the following code example.

# app.py

# Declaring two sets

# Students having Math as a subject
set1 = {'Debasis', 'Shubh', 'Rohit', 'Shouvik'}
# Students having Physics as a subject
set2 = {'Raju', 'Anindita', 'Ayushi', 'Debasis'}

# priting both the sets
print("Set1 is: ", set1)
print("Set2 is : ", set2)
# Now we will find symmetric_difference_update of these two sets
print("Symmetric difference of set1 and set2 is: ",
      set1.symmetric_difference_update(set2))
print("After symmetric_difference_update value of Set1 is: ", set1)
print("After symmetric_difference_update value of Set2 is: ", set2)

Output

Set1 is:  {'Rohit', 'Shubh', 'Debasis', 'Shouvik'}
Set2 is :  {'Anindita', 'Raju', 'Ayushi', 'Debasis'}
Symmetric difference of set1 and set2 is:  None
After symmetric_difference_update value of Set1 is:  {'Rohit', 'Shubh', 'Raju', 'Shouvik', 'Anindita', 'Ayushi'}
After symmetric_difference_update value of Set2 is:  {'Anindita', 'Raju', 'Ayushi', 'Debasis'}

From the above example, we can see that two sets one is details of students having subject Math and the other is details of students having subject Physics.

Now, we can have applied the symmetric_difference_update() method to find the symmetric difference. But as this method returns None, so None is printed when this function is called. But check the value of Set1, we can see that the Set1 is updated with the value of the difference between these two sets.

From these sets we can see that, ‘Debasis’ is having both the subjects, so except Debasis, all the values are updated in Set1.

Finally, Python Set symmetric_difference_update() Method Example is over.

See also

Python set update()

Python Set union()

Python set symmetric_difference()

Python set remove()

Python set pop()

Python set issuperset()

Python set issubset()

Python set isdisjoint()

Python set intersection_update()

Python set intersection()

Python set discard()

Python set difference_update()

Python set difference()

Python set copy()

Python set clear()

Python set add()

2 Comments
  1. Rogger says

    Amazing!

    1. Rogger says

      yeah!

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.