Python Set symmetric_difference() Method
Example
Return a set that contains all items from both sets, except items that are present in both sets:
y = {"google", "microsoft", "apple"}
z = x.symmetric_difference(y)
print(z)
Definition and Usage
The symmetric_difference() method returns a set that
contains all items from both set, but not the items that are present in both
sets.
Meaning: The returned set contains a mix of items that are not present in both sets.
As a shortcut, you can use the ^ operator instead, see example below.
Syntax
Shorter Syntax
Parameter Values
| Parameter | Description |
|---|---|
| set1 | Required. The set to check for matches in |
More Examples
Example
Use ^ as a shortcut instead of
symmetric_difference():
y = {"google", "microsoft", "apple"}
z = x ^ y
print(z)
Related Pages
Tutorial: Python Sets
Tutorial: Join Python Sets
Method: symmetric_difference_update()