set.difference()
Python · Sets
Theory
.difference(other) returns a new set with the items that are in the first set but not in the second. With a = {1, 2, 3} and b = {2, 3, 4}, a.difference(b) is {1}. Order matters here, unlike union and intersection: b.difference(a) is {4}. The operator form is a - b. Neither set is modified.
A related operation, symmetric_difference() or a ^ b, returns items in exactly one of the two sets, {1, 4} here. To remove items from a set in place, use difference_update() or a -= b. Difference runs in about O(len(a)), because each item of a is checked against b by hash. A typical use is comparing two snapshots: new_ids = today - yesterday finds IDs that appeared today, and yesterday - today finds the ones that disappeared.
Why we use it
Comparing two groups to see what is missing or new is a daily task in data validation: records in the source but not the target, customers who did not return, products no longer stocked. difference() gives the answer directly.
When to use it
Use difference() or - for 'in this group but not that one' questions, and swap the order to look the other way. Use ^ for values in only one of the two. For multi-column rows, use a pandas anti-join (merge with indicator=True) instead.
How it works
- Call a.difference(b)
- Start an empty result set
- Take the next item from a
- Is the item in b? Yes: Skip it. No: Add it to the result.
- Return {1}; note b.difference(a) would be {4}
Syntax
set1.difference(set2)Example
a = {1,2,3}
b = {2,3,4}
print(a.difference(b))Key points
- Equivalent to the - operator: a - b
- Order matters: a.difference(b) differs from b.difference(a)
- Returns a new set; the originals are unchanged
- a ^ b gives values in exactly one of the two sets
- difference_update() or -= removes items in place