Python Set symmetric_difference() Method
In this tutorial, we will understand about the python set symmetric_difference() method and its uses.
Python Set symmetric_difference() Method
The Python set symmetric_difference() method returns a new set containing elements that are in either set but not in both. It can also be written using the ^ operator.
The syntax of the symmetric_difference() method is:
1
2
3
set.symmetric_difference(set2)
# or
set1 ^ set2
Python set symmetric_difference() Parameters
The symmetric_difference() method takes one parameter:
- set2: Another set or iterable to find the symmetric difference with.
Here are examples demonstrating the symmetric_difference() method:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Example 1: Basic symmetric difference
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
result = set1.symmetric_difference(set2)
print(result) # Output: {1, 2, 5, 6}
# Example 2: Using operator syntax
numbers1 = {1, 2, 3}
numbers2 = {3, 4, 5}
result = numbers1 ^ numbers2
print(result) # Output: {1, 2, 4, 5}
# Example 3: With empty set
set1 = {1, 2, 3}
set2 = set()
result = set1.symmetric_difference(set2)
print(result) # Output: {1, 2, 3}
The symmetric_difference() method is useful for finding elements unique to each set.
This post is licensed under CC BY 4.0 by the author.