Python set symmetric_difference_update()
Last Updated :
30 Jun, 2023
The symmetric difference between the two sets is the set of elements that are in either of the sets but not in both of them.

Symmetric Difference is marked in Green
symmetric_difference() method returns a new set that contains a symmetric difference of two sets. The symmetric_difference_update() method updates the set by calling symmetric_difference_update() with the symmetric difference of sets.
Python set symmetric_difference_update() Syntax
Syntax: A.symmetric_difference_update(B)
Parameters:
The symmetric_difference takes a single "iterable" as an argument. Iterable should contain hashable object.
Returns:
This method returns None (which indicates absence of a return value). It only updates the set calling symmetric_difference_update() with the symmetric difference of sets.
Python set symmetric_difference_update() Examples
Modifying Sets With Non-Duplicated Elements with Set symmetric_difference_update()
Here A.symmetric_difference_update(B), the elements that are common between set A and set B are removed from A, and only the elements that are unique to each set are kept in A.
Python3
# Python code to demonstrate working of
# symmetric_difference_update()
A = {'p', 'a', 'w', 'a', 'n'}
B = {'r', 'a', 'o', 'n', 'e'}
# result is always none.
result = A.symmetric_difference_update(B)
print('A = ', A)
print('B = ', B)
print('result = ', result)
Output
('A = ', set(['e', 'o', 'p', 'r', 'w']))
('B = ', set(['a', 'r', 'e', 'o', 'n']))
('result = ', None)
Handling Duplicated Elements with Set symmetric_difference_update()
A.symmetric_difference_update(B), the elements that are present in either set A or set B but not in both will be kept in set A. In this case, the resulting set A will contain elements 's' and 'b', which are the elements present in either A or B but not in both. Set B remains unchanged.
Python3
# Python code to demonstrate working of
# symmetric_difference_update()
A = {'s', 'u', 'n', 'n', 'y'}
B = {'b', 'u', 'n', 'n', 'y'}
# result is always none.
result = A.symmetric_difference_update(B)
print('A = ', A)
print('B = ', B)
print('result = ', result)
Output :
('A = ', set(['s', 'b']))
('B = ', set(['y', 'b', 'u', 'n']))
('result = ', None)
Updating Sets with Symmetric Differences in Python
The symmetric_difference_update() method updates the set A by keeping only the elements that are in either A or B, but not in both. In the first example, the list B contains the elements 4, 5, 7, and 8. These elements are not in A, so they are added to A. Elements 1, 2, and 3 are in both A and B, so they are removed from A.
In the second example, the generator object B generates the numbers from 2 to 5. These numbers are not in A, so they are added to A. Elements 4 and 6 are in both A and B, so they are removed from A.
Python3
# Python code to demonstrate working of
# symmetric_difference_update()
A = {1, 2, 3, 4, 5, 6}
B = [4, 5, 7, 8]
# passing argument as list
A.symmetric_difference_update(B)
print("A =", A)
A = {2, 4, 6, 8}
B = (i for i in range(2, 6))
# passing argument as generator object
A.symmetric_difference_update(B)
print("A=", A)
Output
('A =', set([1, 2, 3, 6, 7, 8]))
('A=', set([3, 5, 6, 8]))
Set Symmetric_difference_update() with Handling Unhashable Objects
The Code will raise an error because the symmetric_difference_update() method cannot take a list that contains unhashable objects as an argument. List B contains the element [1, 2, 3], which is a list, and lists are not hashable objects.
Python3
# Python code to demonstrate working of
# symmetric_difference_update()
A = {1, 2, 3, 4, 5}
B = [[1, 2, 3], 4, 5]
# error as b contain one element as list(unhashable object)
A.symmetric_difference_update(B)
print("A =", A)
Output
Traceback (most recent call last):
File "/home/1b4e24cadc3fabcd5f90141964a60e9b.py", line 9, in <module>
A.symmetric_difference_update(B)
TypeError: unhashable type: 'list'
Similar Reads
Python Set Methods A Set in Python is a collection of unique elements which are unordered and mutable. Python provides various functions to work with Set. In this article, we will see a list of all the functions provided by Python to deal with Sets. Adding and Removing elementsWe can add and remove elements form the s
2 min read
Set add() Method in Python The set.add() method in Python adds a new element to a set while ensuring uniqueness. It prevents duplicates automatically and only allows immutable types like numbers, strings, or tuples. If the element already exists, the set remains unchanged, while mutable types like lists or dictionaries cannot
4 min read
Python Set clear() Method Python Set clear() method removes all elements from the set. Python Set clear() Method Syntax: Syntax: set.clear() parameters: The clear() method doesn't take any parameters. Â Return: None Time complexity : The time complexity of set.clear() function on a set with n element is O(n) . Example 1: Pyth
2 min read
set copy() in python The copy() method returns a shallow copy of the set in python. If we use "=" to copy a set to another set, when we modify in the copied set, the changes are also reflected in the original set. So we have to create a shallow copy of the set such that when we modify something in the copied set, change
2 min read
Python Set discard() Function Python discard() is a built-in method to remove elements from the set. The discard() method takes exactly one argument. This method does not return any value. Example: In this example, we are removing the integer 3 from the set with discard() in Python. Python3 my_set = {1, 2, 3, 4, 5} my_set.discar
3 min read
Python Set | difference_update() The difference_update() method helps in an in-place way of differentiating the set. The previously discussed set difference() helps to find out the difference between two sets and returns a new set with the difference value, but the difference_update() updates the existing caller set.If A and B are
1 min read
Python Set difference() In Python, the difference() method is used to find elements that exist in one set but not in another. It returns a new set containing elements from the first set that are not present in the second set. This operation is similar to the subtraction of sets (A - B), where only unique elements from the
2 min read
issuperset() in Python Python Set issuperset() method returns True if all elements of a set B are in set A. Then Set A is the superset of set B. Python issuperset() Method Syntax: Syntax: A.issuperset(B) Parameter: Any other Set to compare with Return: boolean value  Python issuperset() exampleExample 1: Working of issubs
1 min read
Python Set issubset() Method Python set issubset() method returns True if all elements of a set A are present in another set B which is passed as an argument, and returns False if all elements are not present in Python. Python Set issubset() Method SyntaxSyntax: set_obj.issubset(other_set) Parameter: other_set: any other set to
2 min read
Python Set isdisjoint() Method Python set isdisjoint() function check whether the two sets are disjoint or not, if it is disjoint then it returns True otherwise it will return False. Two sets are said to be disjoint when their intersection is null. Python set isdisjoint() Method Syntax: Syntax: set1.isdisjoint(set2) Parameters:
2 min read