Sets and Set Mathematics
Scenario: Two deployment lists contain duplicates and you need common and missing services.
New words, in plain English
- Set - an unordered mutable collection of unique hashable values
- Union - everything in either set
- Intersection - values present in both sets
- Difference - values present in the left set but not the right
Create sets with {...} or set(iterable); the empty set is set(). Use |, &, -, and ^, plus add, discard, and fast membership tests.
Analogy: A Python program is like a clear set of instructions for a careful helper: names label things, indentation groups steps, and errors explain where the helper became confused.
A small, real example
wanted = {"api", "db", "cache"}
running = {"api", "cache"}
print(wanted - running) # {'db'}
Tip: Sets have no stable positional index. Sort them when display order matters.
Goal: Practice this idea in the py-tuples-sets challenge at/labs/python. Fix the broken program, run it yourself, then usecheckto prove the real end state.