Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

set.symmetric_difference

Builds the set of members appearing in exactly one of the two inputs.

Version: Python 3.14; repository image, production runtime not attested

Syntax

s.symmetric_difference(other, /)

Builds the set of members appearing in exactly one of the two inputs.

Common members disappear from the result while the original set stays intact.

2 occurs only on the left, 4 only on the right and 3 on both sides. The shared 3 drops out and the new set contains 2 and 4. The argument can be a list. Sorting merely fixes the order of the display and leaves the receiver intact.

assert {1, 2}.symmetric_difference([2, 3, 3]) == {1, 3}

Examples

s = {2, 3}
print(sorted(s.symmetric_difference([3, 4])))
print(sorted(s))

Result

[2, 4]
[2, 3]

2 occurs only on the left, 4 only on the right and 3 on both sides. The shared 3 drops out and the new set contains 2 and 4. The argument can be a list. Sorting merely fixes the order of the display and leaves the receiver intact.

Sources