A Python set keeps one copy of each distinct value. Use item in group to check membership or set(items) to collapse duplicates. Make an empty set with set(), since {} creates a dictionary. Set members must be hashable, which means strings and integers work as members.
requested = {"notes", "index", "summary"}
ready = {"notes", "index", "notes"}
assert len(ready) == 2
assert "index" in ready
assert requested - ready == {"summary"}
The repeated "notes" appears once. If you need counts or the original order, keep the original list and use the set for membership checks.
Set intersection, union, and difference
a & b is the intersection, containing members found in both sets. a | b is the union, containing members found in either or both. a - b is the difference, containing members from a that are absent from b. a ^ b is the symmetric difference, containing members found in exactly one set. Reverse the operands of a - b to answer the opposite question.
expected = {"reading", "practice", "review"}
submitted = {"reading", "review", "notes"}
missing = expected - submitted
unexpected = submitted - expected
completed = expected & submitted
assert missing == {"practice"}
assert unexpected == {"notes"}
assert completed == {"reading", "review"}
You can also use expected <= submitted to ask if every expected value is present. The operator expresses a subset check, including the case where the sets are equal. Use expected < submitted when there should also be at least one extra member. A good name such as missing helps make the direction clear to the person reading the result.
Keep the original order when order has meaning
set(["tea", "coffee", "tea"]) keeps “tea” and “coffee” but does not promise to iterate in the input order. list(dict.fromkeys(items)) keeps the first occurrence of each hashable item in insertion order. If you want alphabetical presentation, use sorted(set(items)) when the items are comparable. Sorted order and original order are two different requirements.
For partially duplicate records, choose the key that makes two records duplicates from the reader's point of view. Two appointments with the same name may be distinct when their dates differ. A set of tuples keyed by (name, date) can track combinations while you preserve full records in a list. State whether you keep the first or last record for a repeated key. That policy can change a report even when its row count looks plausible.
Add and remove set members
add(item) inserts one item. update(items) inserts each yielded item. remove(item) raises KeyError if the item is absent, while discard(item) treats an already absent member as a successful final state. Choose according to whether the absence itself is evidence of a problem.
Avoid relying on set.pop() to take the oldest or newest item. It removes an arbitrary member. For deterministic processing order, iterate a sorted copy or keep an ordered collection. In a report with mixed member types, supply a meaningful sort key instead of assuming Python compares every combination.
Recognize values that cannot be members
Strings and integers are hashable, and tuples can be hashable if their items are. Lists, dictionaries, and ordinary sets are unhashable. If a list represents a fixed ordered value, a tuple may capture the intended identity. If order and repetition should both be ignored, a frozenset can represent an immutable set and serve as a member of another set.
Some values that look different compare equal. For example, 1 and True compare equal, so putting both into one set yields one distinct member. Validate the input types before deduplicating if your business rules need those categories kept separate. Distinctness follows equality, not how a value was written.
Build a membership index once
Suppose you have a long list of requested titles and a long list of catalog titles. A set gives each request a quick lookup in the catalog. Keep the requests in a list so the result still reflects their order and repeats. Building the index once outside the loop avoids repeatedly doing the same work.
requested = ["Index", "Map", "Index", "Guide"]
catalog = ["Guide", "Map"]
available = set(catalog)
unavailable = [title for title in requested if title not in available]
assert unavailable == ["Index", "Index"]
If the intended result is one message per missing title, use a set difference and sort it for display. If the report is one row per request, the repeated “Index” is meaningful. Keeping the lookup set separate from your preexisting collection makes it easier to communicate which rule you picked.
Check how set methods consume input
The set operator a & b expects both operands to be sets or compatible set types. The corresponding method a.intersection(b) accepts an iterable on the right, such as a list. When readability matters, think about whether construction should validate types early or accept several input forms.
Methods that update a set in place, such as intersection_update, affect every reference to it. Methods such as intersection produce a new set. A function that receives the original collection from a caller should make clear whether it intends to change it. If the caller will continue using the full group afterward, returning a new collection helps avoid a shared-state surprise.
Verify a set-based result
Test an overlapping case, a disjoint case, and an empty collection. When you compare planned and completed work, make sure your test includes both “planned but missing” and “completed but never planned.” It is easy to reverse a subtraction and still return an impressive-looking list. Use a stable presentation order when someone will compare results by eye or in version control.