The Set symmetric_difference() method returns a new set containing all items from both the sets, except common items.
Syntax
my_set.symmetric_difference(set)symmetric_difference() Parameters
Python Set symmetric_difference() method parameters:
| Parameter | Condition | Description |
|---|---|---|
set |
Required | A set for symmetric difference |
symmetric_difference() Return Value
Python Set symmetric_difference() method returns a new set containing the elements that are unique to each of the sets being compared, excluding any elements that are common to both sets.
Examples
Example 1: Symmetric Difference of Two Sets with symmetric_difference() method
A = {'Tom', 'Anna', 'David'}
B = {'David', 'Tom'}
result = A.symmetric_difference(B)
print(result) # Output: {'Anna'}output
{'Anna'}Example 2: Symmetric Difference between Multiple Sets
The symmetric_difference() method does not allow to compute the symmetric difference between multiple sets.
A = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
B = {3, 4, 5, 6, 7}
C = {6, 7, 8, 9, 10}
result = A.symmetric_difference(B, C)output
Traceback (most recent call last):
File "main.py", line 5, in <module>
result = A.symmetric_difference(B, C)
TypeError: symmetric_difference() takes exactly one argument (2 given)However, using ^ operator, you can find symmetric difference between multiple sets.
Example 3: Symmetric Difference between a Set and a List
You can also pass iterables like list: symmetric_difference() first converts the iterables to sets and then computes the symmetric difference.
For example:
A = {'a', 'e', 'i', 'o', 'u'}
B = ['d', 'e', 'f']
print(A.symmetric_difference(B)) # Output: {'f', 'i', 'u', 'a', 'd', 'o'}output
{'f', 'i', 'u', 'a', 'd', 'o'}Example 4: Symmetric Difference between a Set and a Tuple
You can also pass iterables like tuple: symmetric_difference() first converts the iterables to sets and then computes the symmetric difference.
For example:
A = {'a', 'e', 'i', 'o', 'u'}
B = ('d', 'e', 'f')
print(A.symmetric_difference(B)) # Output: {'f', 'i', 'u', 'a', 'd', 'o'}output
{'f', 'i', 'u', 'a', 'd', 'o'}Example 5: Symmetric Difference between a Set and a Dictionary
You can also pass iterables like dictionary: symmetric_difference() first converts the iterables to sets and then computes the symmetric difference.
For example:
A = {'a', 'e', 'i', 'o', 'u'}
B = {1: 'd', 2: 'e', 3: 'f'}
C = {'d': 1, 'e': 2, 'f': 3}
print(A.symmetric_difference(B)) # Output: {1, 2, 3, 'o', 'a', 'i', 'u', 'e'}
print(A.symmetric_difference(C)) # Output: {'o', 'a', 'f', 'i', 'u', 'd'}output
{1, 2, 3, 'o', 'a', 'i', 'u', 'e'}
{'o', 'a', 'f', 'i', 'u', 'd'}Example 6: Symmetric Difference between a Set and a String
You can also pass iterables like string: symmetric_difference() first converts the iterables to sets and then computes the symmetric difference.
For example:
A = {'a', 'e', 'i', 'o', 'u'}
B = 'def'
print(A.symmetric_difference(B)) # Output: {'a', 'f', 'u', 'o', 'i', 'd'}output
{'a', 'f', 'u', 'o', 'i', 'd'}Equivalent Operator ^ for symmetric_difference()
Symmetric difference can be performed with the ^ operator as well, providing an alternative to the symmetric_difference() method.
A = {1, 2, 3, 4, 5}
B = {3, 4, 5, 6, 7}
print(A.symmetric_difference(B)) # Output: {1, 2, 6, 7}
print(A ^ B) # Output: {1, 2, 6, 7}output
{1, 2, 6, 7}
{1, 2, 6, 7}Moreover, you can use the ^ operator with multiple sets:
A = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
B = {3, 4, 5, 6, 7}
C = {6, 7, 8, 9, 10}
print(A ^ B ^ C) # Output: {1, 2, 6, 7}output
{1, 2, 6, 7}