Content · Programming · Python

Python set.symmetric_difference Method: Usage and Examples

set.symmetric_difference(other) returns a new set of elements present in exactly one of the two inputs. It does not mutate the original. See two practical examples, edge cases and common mistakes.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

An inventory comparison detects changes between two snapshots.

What You Need to Understand


set.symmetric_difference(other) returns a new set of elements present in exactly one of the two inputs. It does not mutate the original.

When to Use It


An inventory comparison detects changes between two snapshots.

Step-by-Step Python Example


old = {"A", "B", "C"}
new = {"B", "C", "D"}
print(sorted(old.symmetric_difference(new)))
print(sorted(old))

How the Example Works


The result contains A and D, while the original still contains A, B and C.

Example Output


['A', 'D']
['A', 'B', 'C']
[1, 3]

A Second Example


Compare this smaller variation with the main example, then experiment with your own values.

print(sorted({1, 2}.symmetric_difference({2, 3})))

Important Behavior and Edge Cases


Symmetric difference does not say which members were added versus removed. Use directional differences for that distinction.

Try It Yourself


Compare memberships from two teams and list IDs appearing in exactly one.

Common Beginner Mistakes


  • Symmetric difference does not say which members were added versus removed. Use directional differences for that distinction.
  • Using the wrong collection method for this operation without checking whether it mutates the object, returns a result, or raises on missing data.

Key Takeaways


  • set.symmetric_difference(other) returns a new set of elements present in exactly one of the two inputs.
  • The result contains A and D, while the original still contains A, B and C.
  • Symmetric difference does not say which members were added versus removed.
Share this resourceSend this page to someone who may find it useful.