Track Membership and Uniqueness with Sets
Use sets when the question concerns presence, duplicates, or labels shared between runs rather than position, count, or stored readings.
A dictionary is useful when each sensor label leads to a list of readings. Some questions need less information. To ask whether a label appeared, we need the label but no value beside it.
Keep One Copy of Each Label
A set stores distinct values. Curly braces create a set when at least one value is present:
seen = {"north", "east", "west"}
Order and repetition are not part of a set's meaning. Building a set from a list removes repeated values:
Do not rely on the order used when Python displays a set. Sort it when a report
needs a predictable order. An empty set is written as set(); empty braces,
{}, create an empty dictionary instead.
Set elements must remain stable while stored. The string labels here are suitable. A list is not, because its contents can change.
Q1. Keep distinct sensor labels
What does sorted(set(labels)) return?
labels = ["west", "north", "west", "east"]
Select one choice, then check.
HintSeparate uniqueness from order
First identify the distinct labels. Then sort those labels.
SolutionThree labels remain
The set contains "west", "north", and "east" once each. Sorting them
produces ["east", "north", "west"].
Ask about Membership and Detect Repetition
Membership is the central set question:
The add method inserts a value if it is not already present. Adding an
existing value leaves the set with one copy. Membership and add can expose
repeated labels as the program reads them:
The first occurrence enters seen. A later occurrence finds the label already
there and adds it to repeated.
| Label visited | Already in seen? | seen afterward | repeated afterward |
|---|---|---|---|
"north" | no | {"north"} | set() |
"east" | no | {"north", "east"} | set() |
"north" | yes | {"north", "east"} | {"north"} |
"west" | no | {"north", "east", "west"} | {"north"} |
"east" | yes | {"north", "east", "west"} | {"north", "east"} |
The braces in this trace show membership, not display order.
Compare the Labels in Two Runs
The union | contains labels from either set. The intersection &
contains labels shared by both. The difference - contains labels in the
left set but not the right set, so direction matters.
Q2. Choose the set comparison
Which expression finds labels that appeared in the morning but not in the afternoon?
Select one choice, then check.
HintKeep the direction visible
Begin with morning, then exclude anything also found in afternoon.
SolutionUse morning difference afternoon
morning - afternoon contains "north". Reversing the operands would
instead produce "south".
Derive a Set Comprehension from a Loop
Suppose only available readings should contribute a sensor label. The ordinary loop makes the rule visible:
After the loop is understood, a set comprehension can express the same rule:
Read from the for clause: unpack each measurement, keep it when the reading
is available, and add its sensor to the result set. The two north measurements
still produce one "north" item.
Q3. Build active sensor membership
Complete the set comprehension. The program should display
['north', 'west'] in a predictable order.
Editable Python
Ready to run.
HintFollow the loop version
Produce sensor for each unpacked pair whose reading is not None.
SolutionFilter before adding to the set
A set represents membership without attaching a value to each item. It keeps one copy of a label, supports direct comparisons between groups, and should be sorted only when a report needs predictable display order. The next lesson asks whether two names lead to the same mutable collection.