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

ParameterDescription
other An iterable.

Return value

A new set.

Examples

In one set but not bothTry it yourself
a, b = {1, 2, 3}, {3, 4}
print(a.symmetric_difference(b), a ^ b)

Output

{1, 2, 4} {1, 2, 4}
What changed?Try it yourself
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

VersionChange
Python 3.0Added