You can find Python Symmetric Difference through various functions such as difference(), difference_update(), symmetric_difference(), and symmetric_difference_update().
The symmetric_difference() method gives you the symmetric difference between two sets. It gives you a set of elements that exist in either of the sets but not in both.
You will have to follow this syntax to use set symmetric_difference():
X.symmetric_difference(Y)
Where X and Y are two sets.
Example of how symmetric_difference() works
Here is an example of how we can use symmetric_difference().
# creating sets
x = {'a', 'l', 'l', 'i', 's'}
y = {'e', 'i', 'e', 's', 'y'}
o = {} # empty set
print(x.symmetric_difference(y))
print(y.symmetric_difference(x))
print(x.symmetric_difference(o))
print(y.symmetric_difference(o))
Output:
{'p', 'q', 'l', 'y', 'a', 'e'}
{'p', 'e', 'q', 'l', 'y', 'a'}
{'l', 'q', 'a', 's', 'i'}
{'e', 'p', 'y', 's', 'i'}
Symmetric difference using ^ operator
You can also find the symmetric difference between two sets by using the ^ operator. Here is an example of how.
# sets
x = {'a', 'l', 'l', 'i', 's'}
y = {'e', 'i', 'e', 's', 'y'}
print (x ^ y)
print (y ^ x)
print (x ^ x)
print (y ^ y)
Output:
{'y', 'a', 'e', 'l'}
{'y', 'a', 'e', 'l'}
set()
set()
Questions and Answers
Q1. What is set difference in Python?
In Python, set difference is the difference between the elements of two sets. If P and Q are two sets, then (P-Q) will give you the elements present in P but not in Q. Similarly, (Q-P) will give you the elements present in Q but not in P.
Q2. How do you find the difference in sets in Python?
You can find Python set differences by using the difference functions.
The difference() method is a built-in function that gives us a set containing the difference values (P-Q).
There is another function called symmetric_difference() which gives you the symmetric difference between two sets (the elements that exist in either of the sets but not in both).
# sets
num1 = {45, 34, 7, 36, 23}
num2 = {7, 34, 24, 85, 90}
#using difference()
diff = num1.difference(num2)
print ('Difference is:', diff)
# using symmetric_difference()
symm = num1.symmetric_difference(num2)
print ('Symmetric difference is:', symm)
Output:
Difference is: {36, 45, 23}
Symmetric difference is: {36, 85, 23, 24, 90, 45}
Q3. What is set () in Python?
We use the set() method in Python to convert iterable objects, such as tuples, dictionaries and lists to sets. If you do not specify any parameter in this function, it creates an empty set.
# creating list
lis = [5, 7, 8]
# creating tuple
tup = (4, 2, 9)
# empty set
num = set()
print ('List before conversion:', lis)
print ('Tuple before conversion:', tup)
print ('List after conversion:', set(lis))
print ('Tuple after conversion:', set(tup))
print('Empty set:',num)
Output:
List before conversion: [5, 7, 8]
Tuple before conversion: (4, 2, 9)
List after conversion: {8, 5, 7}
Tuple after conversion: {9, 2, 4}
Empty set: set()
Q4. How do you find the difference between two sets?
There are two ways you can find the difference between two sets.
- By using functions like difference() and symmetric_difference()
- By using operators like – or ^.
Let us look at an example:
# sets
A = {'h', 'e', 'l', 'o'}
B = {'w', 'o', 'r', 'k'}
print ('With difference():', A.difference(B))
print ('With - operator:', A-B)
print ('With symmetric_difference():', A.symmetric_difference(B))
print ('With ^ operator:', A^B)
Output:
With difference(): {'h', 'e', 'l'}
With - operator: {'h', 'e', 'l'}
With symmetric_difference(): {'h', 'l', 'w', 'k', 'r', 'e'}
With ^ operator: {'h', 'l', 'w', 'k', 'r', 'e'}
Leave a Reply