Sets
A set is an unordered collection of unique values, ideal for asking whether something is in it or not.
Used in 20 lessons of the course, from Project 00.
A set stores values without repeats and without a fixed position. You write it with braces and comma-separated values: {"a", "b"}.
Because it has no order, you can't use s[0]. What you do is ask x in s, which is fast, and combine sets: | joins, & intersects and - subtracts.
Watch out: an empty {} is a dict, not a set. For an empty set write set().
In the course, REQUIRED = {"owned_by", "context_window", "chat_model", "on_deprecation_list"} is the set of fields a model must have. A set fits because only which names exist matters, not their order, and you can subtract the ones you have to see which are missing.
Reach for it when: you need values without duplicates, to check membership, or to compare two groups (what's missing, what's extra).
Examples
Common mistake
empty = {}
print(type(empty))Why: Empty braces create a dict, not a set.
How to write it: Write empty = set() for an empty set.
Common mistake
s = {"a", "b"}
print(s[0])Why: A set has no order or positions, so it doesn't support indexes.
How to write it: Use "a" in s to check, or sorted(s)[0] if you need an order.
