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.