Skip to content

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.

Beginner 8 min read·Lesson 119 of 306 in Python Programming

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

Python
only_a = a - b
only_a = a.difference(b, c)
changed = a ^ b
  • - keeps items from the left set.
  • difference() accepts several iterables.
  • ^ keeps items in exactly one set.

Code examples

Direction matters

Python
ordered = {"pen", "pad", "ink"}
delivered = {"pen", "ink", "clip"}
print("Missing:", sorted(ordered - delivered))
print("Extra:", sorted(delivered - ordered))
print("Mismatched:", sorted(ordered ^ delivered))
Output
Missing: ['pad']
Extra: ['clip']
Mismatched: ['clip', 'pad']

Several at once

Python
all_students = {"A", "B", "C", "D", "E"}
print(sorted(all_students.difference(["A"], ("C",), {"E"})))
Output
['B', 'D']

More examples

Comparing two versions of a list

Python
old_menu = {"idli", "dosa", "vada", "upma"}
new_menu = {"idli", "dosa", "poha", "paratha"}
added = new_menu - old_menu
removed = old_menu - new_menu
kept = old_menu & new_menu
print("Added:", sorted(added))
print("Removed:", sorted(removed))
print("Kept:", sorted(kept))
print("Changed items:", len(old_menu ^ new_menu))
pending = {"t1", "t2", "t3"}
pending -= {"t2"}
print("Still pending:", sorted(pending))
Output
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:

TextOutput
Added: ['paratha', 'poha']
Removed: ['upma', 'vada']
Kept: ['dosa', 'idli']
Changed items: 4
Still pending: ['t1', 't3']

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

absent = present - enrolled

Prefer

absent = enrolled - present

Start from the set whose items you want to keep.

Expecting - on lists

Avoid

[1, 2, 3] - [2]

Prefer

[x for x in [1, 2, 3] if x not in {2}]

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

  1. 1
    Given registered and attended participants, print who registered but didn’t attend.
  2. 2
    Find 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?
Practise this module