.symmetric_difference_update()

Anonymous contributor's avatar
Anonymous contributor
Published Jun 3, 2024
Contribute to Docs

In Python, the .symmetric_difference_update() method updates the original set by removing the items present in both sets and adding items unique to each set.

Syntax

The syntax for using this method is:

set1.symmetric_difference_update(set2)

Or in a more compact form :

set1 ^= set2
  • set1: Refers to the original set.
  • set2: This is the set to be compared with the original set.

Example

The following example removes items present in both store1 and store2 and adds unique items from store2 to store1 set:

store1 = {'apple', 'mango', 'lichi', 'orange', 'banana'}
store2 = {'strawberry', 'banana', 'pear', 'raspberry'}
store1.symmetric_difference_update(store2)
print(store1)

The above code will produce a modified store1 set as follows:

{'apple', 'mango', 'lichi', 'orange', 'strawberry', 'pear', 'raspberry'}

Note: A set in Python is unordered. This means that the order of items can change in the above output.

Codebyte Example

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Python on Codecademy