×

Python Set symmetric_difference_update() method

Python Set symmetric_difference_update() method

The set.symmetric_difference_update() method in Python updates the original set by removing items that are present in both sets and inserting the other items of the set.

Syntax

set.symmetric_difference_update(set1)

Parameter

set- This parameter represents the first set that is used for checking the matches.

set1- This parameter represents the second set that is also used for checking the matches.

Return

None

Example 1

# Python program explaining
# the set.symmetric_difference_update() method
#initializing set1
set1 = {"mango", "banana", "apple","tomato"} 
print("Set 1:",set1)
# initializing set2
set2 = {"potato", "spinach","Carrot", "apple"} 
print("Set 2:",set2)
# updates the original set by removing items that are present in both sets
set1.symmetric_difference_update(set2) 
print("\nAfter the symmetric_difference_update() method...\n") 
# printing the updated new set
print("Updated Set 1:",set1)
# no change occurs at second set
# the second set remains the same
print("Set 2:",set2) 

Output

Set 1: {'banana', 'apple', 'tomato', 'mango'}
Set 2: {'spinach', 'potato', 'Carrot', 'apple'}
After the symmetric_difference_update() method...
Updated Set 1: {'potato', 'tomato', 'mango', 'banana', 'spinach', 'Carrot'}
Set 2: {'spinach', 'potato', 'Carrot', 'apple'} 

Example 2

# Python program explaining
# the set.symmetric_difference_update() method
# initializing the set1
set1 = {1,2,3,4,5,6} 
# initializing the set2
set2 = {2,7,8,6,5}
# updates the original set by removing items that are present in both sets
set1.symmetric_difference_update(set1) # passing same sets in both the arguments 
# printing the updated set
# will return set() as it deletes the common values.
print("Updated Set 1:", set1)
# no change in set2
print("Set 2:",set2) 

Output

Updated Set 1: set()
Set 2: {8, 2, 5, 6, 7}

Related Topics

Function annotations() in Python

What is Function Annotations? Function annotations provide a way related to different parts of a function at compile time with arbitrary Python expressions. It doesn’t have life in Python runtime execution....

3 minutes read.

XXhash Python

Python: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

6 minutes read.

Spyder (32-bit) - Free download

Spyder is a free and open-source scientific environment created by and for Python engineers, scientists, and data analysts. It is a robust scientific environment created in Python by and for...

3 minutes read.

Continue and Pass Statements in Python

Difference between continue and pass statements in Python The tasks can be repeated and automated efficiently using loops in Python. Sometimes we have to exit from the entire loop completely, we...

2 minutes read.

Python Packages

A package is a collection of Python modules that share a similar namespace and are generated by putting all of the modules in a single directory with certain special files...

6 minutes read.

CSV Write in Python

What is meant by CSV? CSV stands for Comma Separated Values. The name itself defines its purpose. CSV arranges the data in the form of tables and stores the organized data in...

3 minutes read.

Python program to print array element present at even position

Python program to print array element present at even position In this program, we'll see a Python program that prints the elements of an array that are in even positions. We...

1 minute read.

ComboBox in Python

Python includes several graphical user interface (GUI) libraries, including PyQT, Tkinter, Kivy, WxPython, and PySide. Tkinter is the most commonly used GUI module in Python because it is simple and...

2 minutes read.

Python zip() Function

Python zip() Function The zip() function makes an iterator that aggregates elements from each of the iterables and returns an iterator of tuples. Syntax zip(*iterables) Parameter iterables: Iterator objects that will be joined together Return This function...

1 minute read.

Python List Methods

Python List Methods Python has a set of built-in methods that you can use on lists or arrays. Following are all of the methods of list objects: Methods Explanation append The list.append() method...

3 minutes read.

Python String endswith() method

Python String endswith() method The string.endswith() method in Python returns a Boolean value True if the string ends with the specified suffix, otherwise it returns False. Syntax endswith(suffix[, start[, end]]) Parameter suffix – This parameter represents a string or tuple...

2 minutes read.

How to create a class in python

In any programming language, a class is a user-defined plan or blueprint using which objects or instances of the class are created. You may wonder why we need classes in programming....

5 minutes read.

Python Identifiers

Identifiers in Python User-defined names are identifiers in Python that are used to name variables, functions, classes, modules, and other things. You can create Python identifiers using these rules: As an identifier...

4 minutes read.

ER diagram of the Bank Management System in python

What is an ER diagram? The full form of the ER diagram is the Entity Relationship diagram. This diagram is used in database management systems to have a rough idea of...

3 minutes read.

GET and POST requests using Python

‘GET’ and ‘POST’ are two request methods of Hypertext Transfer Protocol (HTTP). What is HTTP? HTTP stands for Hypertext Transfer Protocol. It is a collection of protocols which makes communication between client...

4 minutes read.

Creating Web Application in python

Web Application :  Web Application is an application software that runs in web browser . Software programs will run on operating system. Whereas Web Applications will run on World Wide Web...

5 minutes read.

How to set font for Text in Python

As a tuple with the font family as the first component, a size in point as the second, and possibly a string with one or more of the style modifiers...

5 minutes read.

Python Permutations and Combinations

In mathematics, we all studied what is meant by permutations and combinations. “Permutations” define the way of arranging the elements in sequential order. “Combinations” mean the way of selecting the...

7 minutes read.

Python program to perform the arithmetic operation

Python program to perform the arithmetic operation This program will write a code to perform some basic arithmetic operations like addition, subtraction, multiplication, exponent, modulus, and division. Here we first need...

2 minutes read.

Python Project Ideas

One of the most widely used programming languages today is Python. This pattern appears set to continue through 2023 and beyond. Therefore, working on some current Python project ideas is the...

10 minutes read.