Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

method
python
set
symmetric
update

What is the set symmetric_difference_update() method in Python?

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The symmetric_difference_update() method in Python for two elements, A and B, is used to return the set of elements that are contained in A and B, but not common in both of them, and it also updates the set calling it. In other words, it is used to return the symmetric difference of two sets and update the set calling it.

Syntax

A.symmetric_difference_update(B)
Parameter value

Parameter

Description

A

This is required. It is one of the sets with which we want to find a symmetric difference with the second set. This is the updated set.

B

This is required. It is one of the sets with which we want to find a symmetric difference with the second set.

Return value

The symmetric_difference_update() method returns no value. Rather, it updates the set that calls it.

Example 1

Now let’s create two sets, A and B, and then use the symmetric_difference_update() method to obtain the update from both sets.

Code

A = {1, 2, 3, 4}
B = {2, 3, 6, 5}
A.symmetric_difference_update(B)
# to return the updated set A
print('The symmetric difference between A and B is:', A )
# to return the set B
print('B still remains:', B)

Explanation

  • Line 1: We create set A.
  • Line 2: We create another set, B.
  • Line 4: We use the symmetric_difference_update() method to take the symmetrical difference between the two sets, A and B.
  • Line 6: We return the updated set A, which contains elements found in both sets, but are not common to both of them.
  • Line 8: We return the value of set B.

Note: We can see that the return value of the symmetric_difference_update() method is actually none (that is, no result). However, it only updates the value of set A.

Example 2

Let’s try out another example in the code below.

Code

A = {'a','b','c','d'}
B = {'a','f','g'}
A.symmetric_difference_update(B)
# to return the updated set A
print('The symmetric difference between A and B is:', A )
# to return the set B
print('B still remains:', B)

Summary

The "symmetric-difference-update() method returns the symmetric difference between two sets, while simultaneously updating the set that called it.

RELATED TAGS

method
python
set
symmetric
update

CONTRIBUTOR

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring