Methods and Functions

Python Set symmetric_difference_update()

In Python, we have various Set class methods to perform different operations. One of them is the Python set symmetric_difference() where the function returns only the unique values of the 2 sets by discarding the common values between them. The Python symmetric_difference_update() is similar to this function. It only varies by updating the calling set. We will learn about this function in the article below.

Python symmetric_difference_update() function

Definition

  • The Python symmetric_difference_update() function is used to detect the uncommon elements in the given two sets and update the elements in the specified set.
  • Python symmetric_difference_update() method updates the calling set and returns a set containing distinct elements found in both sets, and removing the common elements.

In Laymann terms, the intersecting elements of 2 sets are disregarded and the uncommon elements are added to the calling set. For example,

A.symmetric_difference_update(B) will update the set A with only those values that are unique in both the sets.

Syntax of Python symmetric_difference_update()

The syntax followed by this function is:

                    

set1.symmetric.difference.update(set2)

symmetric_difference_update() Parameters

As an argument, the symmetric_difference_update() takes a single iterable. Iterable objects should be hashable.

Return value from symmetric_difference_update()

This function does not return any value. Rather, it updates the set calling it.

Note – If we do not want to update the original set, we may use the symmetric_difference() method.

Example 1: Working of symmetric_difference_update()

In the example below, we have two sets, num1 and num2. We are calling the symmetric_difference_update() method, which determines the symmetric difference between num1 and num2, which contains the elements that are either in Set num1 or in Set num2 but not both, and then updates the calling set num1 with the symmetric difference.

Example 1

                    

# Python program to illustrate symmetric_difference_update()
# set 1
num1 = {5, 10, 15, 20}
# set 2
num2 = {5, 15, 25}

print('Before calling symmetric_difference_update:')
print('num1 Set:', num1)
print('num2 Set:', num2)

# calling function
num1.symmetric_difference_update(num2)

print('After calling symmetric_difference_update:')
print('num1 Set:', num1)
print('num2 Set:', num2)

Output

                    

Before calling symmetric_difference_update:
num1 Set: {10, 20, 5, 15}
num2 Set: {25, 5, 15}

After calling symmetric_difference_update:
num1 Set: {10, 20, 25}
num2 Set: {25, 5, 15}

Example 2

                    

# Python program to illustrate symmetric_difference_update()
X = {'a', 'x', 't', 'w', 'v'}
Y = {'w', 'r', 'h', 'v', 'a'}

X.symmetric_difference_update(Y)
print('X = :', X)
print('Y = :', Y)

Output

                    

X = : {'h', 'r', 't', 'x'}
Y = : {'a', 'h', 'r', 'v', 'w'}

Frequently Asked Questions

Q1. What is set() in Python?

A set is a group of elements that are not in any particular sequence. Every set element must be unique (no duplication) and immutable (cannot be changed). A set, on the other hand, is mutable. We have the ability to add and remove items from it.  Sets can also be utilized to conduct mathematical set operations such as union, intersection, and symmetric difference, among others.

Python’s set() function is used to create a set out of the iterable passed to the function.

Example

                    

# using set() function
names = ['Ricky', 'Daisy', 'Lily', 'John']
# creates set from list
names_set = set(names)
print('Created set:', names_set)

Output

                    

Created set: {'Lily', 'Daisy', 'Ricky', 'John'}

Q2. How do you initialize a set in Python?

In Python, a set is created by placing all the elements/items inside the curly braces {}, each value separated by a comma, or by using the Python set() built-in function.

The set can contain any number of items that may be of different data types such as integer, float, tuple, string. But it cannot contain mutable elements such as lists, dictionary’s and sets themselves.

Example

                    

my_set = {1, 2, 'Python', 'Program'}
print('Set created:', my_set)

car_list = ['Audi', 'BMW', 'Tata', 'Ford']
my_set = set(car_list)
print('Set created:', my_set)

Output

                    

Set created: {1, 2, 'Program', 'Python'}
Set created: {'Ford', 'Audi', 'Tata', 'BMW'}

Q3. What is a set and frozen set in Python?

  • Set – A set is a group of elements that are not in any particular sequence. Every set element must be unique (no duplication) and immutable (cannot be changed). Sets can also be utilized to conduct mathematical set operations such as union, intersection, and symmetric difference, among others.
  • Frozen set – In Python, a frozenset is the same as a set, except that frozensets are immutable, which implies that once generated, elements from the frozenset cannot be added or removed. This function accepts any iterable object as input and turns it to an immutable object. It is not assured that the order of the elements will be retained.
Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.