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