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()
