×

Python Set symmetric_difference() method

Python Set symmetric_difference() method

The set.symmetric_difference() method returns a new set, which is the symmetric difference of two sets. The returned set contains only the unique items and, hence, deleting the common elements of both the sets.

Syntax

set.symmetric_difference(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 used for checking the matches.

Return

This method returns a set that contains only the unique items from both the sets thus, deleting the common elements or intersection from both the sets.

Example 1

# Python program explaining
# the set.symmetric_difference() method
#initializing set1 
set1 = {"mango", "banana", "apple","tomato"}
print("Set 1:",set1)
# initializing set2 
set2 = {"potato", "spinach","Carrot", "apple"}
print("Set 2:",set2)
# lisitng all the items of both the sets 
# and deleting the common elements from the sets 
SymmetricDifference= set1.symmetric_difference(set2) 
print("The symmetric difference for both the sets are:\n",SymmetricDifference) 

Output

Set 1: {'mango', 'tomato', 'banana', 'apple'}
Set 2: {'Carrot', 'spinach', 'apple', 'potato'}
The symmetric difference for both the sets are:
{'tomato', 'spinach', 'mango', 'Carrot', 'potato', 'banana'} 

Example 2

# Python program explaining
# the set.symmetric_difference() method
#initializing the vowels set
vowels = {"a", "e", "i","o","u"} 
print("Vowels are:",vowels)
# initializing any random alphabet
# here alphabet "a" is repeated twice 
randomAlphabets = {"a", "h","a", "e","f"}
print("Random Alphabets are:",randomAlphabets)
# lisitng only the unique items of both the sets  
# and deleting the common elements present in both the sets
SymmetricDifference= vowels.symmetric_difference(randomAlphabets) 
print("The symmetric difference for both the sets are:\n",SymmetricDifference) 

Output

Vowels are: {'i', 'u', 'o', 'e', 'a'}
Random Alphabets are: {'a', 'e', 'h', 'f'}
The symmetric difference for both the sets are:
 {'o', 'i', 'f', 'h', 'u'} 

Example 3

# Python program explaining
# the set.symmetric_difference() method
# initializing the set1
set1 = {1,2,3,4,5,6} 
# initializing the set2
set2 = {2,7,8,6,5}
# The '^' operator return the same output 
#as returned by the set.symmetric_difference() method 
print("The symmetric_difference between set1 and set2..")
print("By using '^' operator:",set1 ^ set2)
print("By using set() method:",set1.symmetric_difference(set2))
print("\nThe symmetric_difference between set2 and set1..") 
print("By using '^' operator:",set2 ^ set1)
print("By using set() method:",set2.symmetric_difference(set1))
# passing the same set values 
print("\nThe symmetric_difference between set1 and set1..")
print("By using '^' operator:",set1 ^ set1)
print("By using set() method:",set1.symmetric_difference(set1)) 

Output

The symmetric_difference between set1 and set2..
By using '^' operator: {1, 3, 4, 7, 8}
By using set() method: {1, 3, 4, 7, 8}
The symmetric_difference between set2 and set1.. 
By using '^' operator: {1, 3, 4, 7, 8}
By using set() method: {1, 3, 4, 7, 8}
The symmetric_difference between set1 and set1.. 
By using '^' operator: set()
By using set() method: set() 

Related Topics

Tuple in Python

Tuples Tuples are the same as the list but as we know that lists are mutable means we can change the data from it. In the tuple we cannot change the...

6 minutes read.

Python String upper() method

Python String upper() method The string.upper() method in Python returns a copy of the string converted to uppercase. Syntax string.upper() Parameter NA Return This method returns a copy of the string converted to uppercase. Example 1 # Python...

1 minute read.

Character Set in Python

A collection of legal characters that will recognize by a programming language known as a Character set. Taking python programming language as an instance. The set of characters supported by the...

6 minutes read.

How to Read html page in python

Html is a Hyper Text Markup language is a standard language used for creating webpages. HTML is the name of the language used to describe the construction of Web pages....

4 minutes read.

Python BytesIO

Python Programming Language 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...

3 minutes read.

Python Set intersection_update() method

Python Set intersection_update() method The set.intersection_update() method in Python removes the items that is not present in both sets. It is different from the set.intersection() method, because the intersection()method returns a new set, with only  the common elements...

2 minutes read.

Not supported between instances of str and int in python

In this article, you are going to learn why the type error occurs between instances of string and integer datatypes. Also, you will know what kind of error is the...

6 minutes read.

Python comment symbol

Python programmers frequently use the comment system because, without it, things may quickly become very perplexing. The developers' helpful information is provided in the comments, which helps the reader understand...

3 minutes read.

Mrcnn Python

Python Programming Language 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...

6 minutes read.

Python History

Python is one of the top trending, widely-used programming languages. Python is a general-purpose programming language. Python language is known for its features. Some features are given below: SimplisticFree and open...

4 minutes read.

How to run Python code from the command prompt

The Windows operating system's command-line interpreter is CMD or Command Prompt. The "MS-DOS Prompt" is comparable to Command.com, used in DOS and Windows 9x computers. It is similar to Unix...

3 minutes read.

Python sort() function

Python provides many built-in functions for solving many problems that arise in different situations in programs. One of such methods is the sort () method. In this article, the syntax...

4 minutes read.

Image to NumPy Arrays in Python

Image is a simple process of working model confined to machine learning, Python works with the image in the data format of width and height i.e. Images are excelled into...

3 minutes read.

Latest Project Ideas using Python 2024

Python is one of the well-known languages for programming in the contemporary domain of computer science. The demand to adopt Python for IT purposes is steadily increasing because of its...

7 minutes read.

Hypothesis Testing in Python

Hypothesis Testing in python is widely used along with statistics. Many libraries in Python are very useful for statistics and machine learning. Libraries like numpy, scripy etc. help in hypothesis testing in...

12 minutes read.

Python IDE names

Python is one of the most renowned programming languages. It has different execution conditions and a great many compilers to execute the python programs, e.g., PyCharm, PyDev, Jupyter Notebook, Visual...

6 minutes read.

Python Marshmallow

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...

3 minutes read.

Python math.cos and math.acos function

Math.cos() function In Python, the Math module is used for performing the mathematical operations. It includes the math.cos() function that is used for obtaining the cosine value of an angle in...

3 minutes read.

Arithmetic Expressions in Python

What is Python Expression? Expressions are collections of operands and operators. Python expressions are translated by the Python interpreter into some value or outcome. In Python, an expression is made up...

13 minutes read.

Defaultdict in Python

In this tutorial, we will study What is defaultdict in Python We will understand it with the aid of certain examples. Before this, we will have a look at dictionaries...

3 minutes read.