×

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

How to append a string in python

Adding or appending a string to another string is easy in Python. It is called concatenation and is a basic concept of strings in almost all programming languages. Ways to Append...

4 minutes read.

Permutations in Python

Recursion Basic idea: for numbers of length N. N-1 items are chosen at random between 0 and then generate permutations using the remaining N-1 elements in a recursive fashion. Once you've done...

4 minutes read.

What is Collaborative Filtering in ML, Python

Introduction Contents recommendation is a useful tactic for almost any specific technology looking to increase interest, but it frequently calls for a lot of user data and perhaps laborious content tagging...

3 minutes read.

Multiple Linear Regression using Python

Linear Regression: Linear regression is a method that models the relationship between a dependent variable and one or more independent variables; in other terms, that models the relationship between a target...

6 minutes read.

Python Variable Scope with Local & Non-local Examples

This article will examine Python's global, local and non-local variables and show you how to use them to write code without problems. Let's quickly review what a variable in Python is...

8 minutes read.

Python String rfind() method

The string. rfind() method in Python returns the highest index in the string if the substring sub is found else it returns -1 if the substring is not found. Syntax string.rfind(sub [,start [,end]]) Parameter sub: This parameter...

2 minutes read.

Python - Binomial Distribution

Introduction Definition of the Binomial Distribution The method of counting how many instances of a specific event there have been is called the binomial distribution. It will outline the possible outcomes or...

4 minutes read.

Python Pass Statement

The pass statement is a null statement. The difference between pass and comment is that comment is ignored by the interpreter, whereas pass is not. The pass statement is typically used...

3 minutes read.

Python SQLite

SQLite It is an RDBMS (Relational Database Management System). It is an embedded, serverless, transactional SQL database engine. It is an open-source application. It is named SQLite because of its lightweight....

3 minutes read.

Difference between Package and Module in Python

What are Python modules? A file with the “.py” suffix that includes Python or C executable code is known as a module. Multiple Python commands and expressions make compose a module....

3 minutes read.

Python Modules List

Python is like an ocean. If you have been working with Python, you might’ve already heard the word module. When we solve a problem in mathematics, there will be several...

3 minutes read.

Python List append() Method

Python List append() Method The list.append() method in Python adds or appends an item to the end of the list. Syntax list.append(x) Parameter x: It is a required parameter that represents an element of any type (string, number,...

1 minute read.

Python sklearn train_test_split

Sklearn: One of the most beneficial open-source libraries for learning algorithms in Python is, without a doubt, Sklearn or Scikit-Learn. The most effective tools for statistical modeling and machine learning are all included in...

6 minutes read.

ModuleNotFoundError No module named 'mysql' in Python

mysql module not found is an error that occurs in python. It appears like ModuleNotError: No module named 'mysql.' This error occurs when you forget to install a module called...

3 minutes read.

Python divmod() function

Python divmod() function The divmod() function in Python returns a tuple containing the quotient  and the remainder when parameter ‘a’ (divident) is divided by parameter ‘b’ (divisor). Syntax: divmod(a, b) Parameter a: This parameter represents a number you...

1 minute read.

Pillow Python introduction and setup

Introduction Advanced image processing implies handling the picture carefully with the assistance of a PC. Utilizing picture handling we can perform activities like upgrading the picture, obscuring the picture, separating text...

4 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 Recursion

Recursion is one of the most interesting yet important concepts of any programming language. If you want to be a good programmer or data scientist, then you should better have...

4 minutes read.

Python Ways to find nth occurrence of substring in a string

Introduction In Python, a string is a collection of characters that can be employed to conduct additional operations. In Python, a substring is a group of characters that are a part...

4 minutes read.

Image to Text in python

Processing out information from an image is a very big task in all the fields of work such as in development and business sector. The process of converting an electronical...

3 minutes read.