Python 3 reference · Set Methods
Python Set symmetric_difference() Method
Return a new set with elements in either set but not both. The same as a ^ b.
Python 3 Since 3.0
Syntax
set.symmetric_difference(other)
Parameters
| Parameter | Description |
|---|---|
other |
An iterable. |
Return value
A new set.
Examples
a, b = {1, 2, 3}, {3, 4}
print(a.symmetric_difference(b), a ^ b)
Output
{1, 2, 4} {1, 2, 4}old = {"a.py", "b.py"}
new = {"b.py", "c.py"}
print(sorted(old ^ new)) # files added or removed
Output
['a.py', 'c.py']
Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
set.symmetric_difference_update()· Update the set in place to keep elements in either set but not both. The same as a ^= b.set.difference()· Return a new set with the elements of this set that are not in the others. The same as a - b.set.union()· Return a new set with all elements from this set and the others. The same as a | b.- Python Sets · tutorial