Module 8 · Python Data Structures — topic 16 of 22
Difference
The difference a - b keeps the items of a that are not in b. Learn its direction, difference with several sets, symmetric difference, and practical uses for finding missing or extra items.
Learning objectives
- Compute differences in the right direction
- Use difference() with several iterables
- Use symmetric difference to find items in exactly one set
Definition
The difference a - b keeps the items of a that are not in b. Learn its direction, difference with several sets, symmetric difference, and practical uses for finding missing or extra items.
Introduction
The difference a - b contains the items of a that do not appear in b. It answers “what does a have that b doesn’t?” Unlike union and intersection, the order of the operands matters.
Why it matters
Reconciling lists is a common business task: which ordered items weren’t delivered, which enrolled students didn’t attend, which old features were removed in a new version. Each is a set difference.
Detailed explanation
a - b keeps items in a that are not in b; b - a keeps items in b that are not in a.
a.difference(b, c) removes the items of every argument, which can be any iterable.
a -= b or a.difference_update(b) removes items from a in place.
a ^ b (symmetric difference) contains items in exactly one of the sets; it equals (a - b) | (b - a).
If b contains everything in a, then a - b is empty.
Differences are a quick way to compare two versions of a list: what was added (new - old) and what was removed (old - new).
Syntax
- - keeps items from the left set.
- difference() accepts several iterables.
- ^ keeps items in exactly one set.
Code examples
Direction matters
Missing: ['pad'] Extra: ['clip'] Mismatched: ['clip', 'pad']
Several at once
['B', 'D']
More examples
Comparing two versions of a list
Added: ['paratha', 'poha'] Removed: ['upma', 'vada'] Kept: ['dosa', 'idli'] Changed items: 4 Still pending: ['t1', 't3']
Step-by-step explanation
- new_menu - old_menu finds dishes that were added.
- old_menu - new_menu finds dishes that were removed.
- & finds dishes on both menus, and ^ counts all changed dishes.
- pending -= {"t2"} removes a completed task in place.
Expected output
Running “Comparing two versions of a list” prints exactly:
Practical applications
- Attendance systems find absent students with enrolled - present.
- Release notes list added and removed features by comparing versions.
- Inventory reconciliation finds missing stock with expected - counted.
Best practices
- Name differences by meaning: missing, extra, added, removed.
- Use ^ to get everything that changed in one step.
- Use a comprehension when you need to keep the original order.
Common mistakes
Wrong direction
Avoid
Prefer
Start from the set whose items you want to keep.
Expecting - on lists
Avoid
Prefer
Lists don’t support -; use sets or a comprehension (which also keeps order).
Key takeaways
- a - b keeps items of a not in b; direction matters.
- difference() accepts several iterables; -= updates in place.
- a ^ b keeps items in exactly one set.
Try it yourself
- 1Given registered and attended participants, print who registered but didn’t attend.
- 2Find the letters that appear in exactly one of the words "listen" and "silent".
Finished reading? Mark it complete to update your dashboard.
Suggested practice
13 questions cover the “Python Data Structures” module. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?
- BeginnerWhat is printed?
- BeginnerWhat is printed?