×

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

Append key Value to Dictionary in Python

The Python dictionary is one of the built-in data types. Elements of dictionaries are key-value pairs. In Python, there are numerous ways to add dictionaries. Let's examine some of the...

9 minutes read.

What is the Python Global Interpreter Lock?

Introduction When working with processes, Python employs a form of process lock called the Global Interpreter Lock (GIL). Python typically executes a collection of typed statements using just one thread. It...

4 minutes read.

How to Pass a list as an Argument in Python

Lists in Python A list is a datatype in python which is used to store the data in a sequence. The lists are mutable; that is, we can change the values...

3 minutes read.

First Unique Character in a String Python

This article aims to introduce you to strings and how to create a string in Python, and then we will solve a simple yet exciting DSA (Data Structures and Algorithms)...

3 minutes read.

Python Interface

When creating an application, it is important to continuously keep track of its changes. As an application grows, sometimes it gets hard to manage its updates and changes. Often, you...

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.

wxPython Panel class

wxPython Panel class The Widgets which is shown in the frame of GUI window such as text box, buttons, static text etc. are put inside the panel class of the wxpython...

2 minutes read.

Kite Python

Kite in Python: The Kite is a package provided by the python programming language; it works with the help of artificial intelligence and helps us write code inside the visual studio....

3 minutes read.

One Liner If-Else Statements in Python

Before learning one liner if else Python, let us first know what Python is. Python is a broadly helpful, object-oriented and dependable language having a large number of utilizations from...

4 minutes read.

List in Python

What is List in Python In Python, lists are used to store the multiple values in one variable. We can say that list is the collection of similar as well as...

3 minutes read.

Enumerate() Function in Python

The enumerate() function in Python is used to convert a data collection object into an enumerate object. It returns an object that contains a counter as a key for each...

3 minutes read.

Python String

Python String Python string is considered as the most popular data type present in Python language. In Python, string data type is the collection of characters, letters, white spaces etc. written...

31 minutes read.

Difference between Perl and Python

Control and presently utilized for a great many undertakings, including framework organization, web improvement, network programming, and GUI improvement, and the sky is the limit from there. About Perl? Perl is a...

4 minutes read.

Python String lower() method

Python String lower() method The string.lower() method in Python returns a string where all characters are lower case. Syntax string.lower() Parameter NA Return This method returns a string where all characters are lower case. Example 1 # Python...

1 minute read.

Python Data Structures

Python Data Structures: Python is a programming language used worldwide for various fields such as building dynamic websites, artificial intelligence and many more. However, there is data that plays a...

18 minutes read.

Python Slice from Last Occurrence of K

Introduction We already know that in Python, cutting produces a sub-string out of a string. The variables start, stop, and step are used to set the slicing range. When dealing on...

3 minutes read.

Scraping data in python

Data scraping is a technique in which one program extracts a set of data from the output of another program. Web scraping is the most common application of this technique....

6 minutes read.

Notepad++ For Python

In this article, you are going to learn what notepad++ is and how it is integrated with python to use the python programming language. You can also learn how to...

4 minutes read.

How to uninstall python

To un-install Python, we need to follow different procedures in different operating systems. In this article, we will discuss the un-installation process of Python in Windows, Mac, and Linux operating...

4 minutes read.

Python String join() method

Python String join() method The String.join() method in Python concatenates each element of an iterable (such as list, string and tuple) to the given string and returns the concatenated string. Syntax string.join(seq) Parameter Seq: This...

1 minute read.