Lists, dicts, sets, and choosing between them
By the end of today you can pick the right collection for a problem and defend the choice, and you can say roughly how expensive a lookup is in each one without having measured it.
YesterdayOn Day 4 you learned that functions receive and return values. Today you meet the values they most often carry.
TomorrowTomorrow things go wrong on purpose, and you learn to read what Python tells you when they do.
Why this matters
Choosing the wrong collection is a bug that only appears at scale. The dict you reach for today is the same idea as a cache on Day 60 and a database index on Day 43.
- Lists
- Ordered and indexed, where searching means checking every element.
- Dictionaries
- Key to value, where hashing makes lookup barely slow as it grows.
- Sets
- Membership and uniqueness, on the same hashing idea as a dict.
- Choosing a collection
- Picking a structure is choosing which operation you want to be cheap.
See it first
5 minOne small thing, worked through here before you go anywhere. Guess the answer before you open it.
A nightly job checks 50,000 orders against the 50,000 customers who are still active. One line does the checking, and it was fine on the test data.
# 50,000 ids, in a list
active = [c.id for c in customers()]
# 50,000 orders
for order in orders():
if order.customer_id in active:
process(order)This runs in under a second on 100 test rows. Roughly how long does it take on the real 50,000?
Decide before you open it. Being wrong here is the point.
Show me what happens
Minutes, and it gets four times worse every time the business doubles. in on a list checks the items one after another until it finds a match. On average that is 25,000 comparisons, and it happens once per order. 50,000 orders times 25,000 comparisons is over a billion. Change one character and the whole thing finishes instantly: active = {c.id for c in load_customers()} Curly braces make a set. A set does not look through its items, it computes where the value would be and looks only there, which takes the same time whether it holds ten ids or ten million. Same loop, same reading, same result, roughly a billion fewer comparisons.
Where you meet this at work
This is the most common performance bug in working code, and it never shows up in testing because test data is small. It is also the single idea behind a database index on Day 43 and a cache on Day 60: stop searching, start looking straight at the answer.
Learn it
70 minCopy this into Claude or ChatGPT. It quizzes you before it explains anything, which is deliberate. The resources under it are how you check what it told you.
Today's Master Prompt
Free · sign inA prompt written for this day alone: your level, the exact scope, what to leave out, and an instruction to quiz you before it explains anything. Paste it into Claude or ChatGPT and it teaches you today's material.
Check it against something that is not a model
An assistant can be fluent and wrong, and on a topic you met today you will not catch it. These cover the same ground and were made by people who do this for a living, so they are what you hold the explanation up against. They are other people's work and we only link to them, so judge them for yourself.
5 hand-picked resources
Free · sign inVideos, official docs and articles covering the same ground, each opened and annotated by hand. They are what you check the assistant against on a day you cannot yet catch it being wrong.
Build it
40 minBuild a list of 100,000 strings and a set of the same strings. Time how long it takes to check membership for a value that is not present, in each. Then write a small program that counts how many times each word appears in a paragraph, using a dict, and explain why a list would have been the wrong choice.
Recall it
20 minAnswer out loud, reveal, then mark honestly whether you had it. That score is the only thing on this page you do not get to choose.
4 recall questions
Free · sign inQuestions you answer from memory, then grade yourself against the real answer. The score is carried into the mastery rating below it, so an honest miss cannot quietly become a tick.
Rate it
Finishing a day and understanding it are two different things, and this page counts them separately.
Mastery tracking
Free · sign inRate yourself against five named criteria per concept. Completion and mastery are tracked separately, and anything you rate shakily comes back automatically on a spaced schedule.
Recap
- 01Lists keep order and position; searching one means checking every element
- 02Dicts and sets hash a key so lookup barely slows as they grow
- 03Keys must be immutable, which is why tuples work and lists do not
- 04Choosing a collection is choosing which operation you want to be cheap
Your progress
Free · sign inMark days complete, pick up where you left off across devices, and watch completion and mastery diverge. Free, and the account exists only so ninety days of work cannot vanish with a cleared browser.