Exercise 8 of 10 · Lists
Packing List
What you will make
A travel packing checklist with a banner, one tick-box line for every item you put on the list, and a footer that counts them for you.
The one new idea: A list holds several values in order, and a for loop walks through them
Real programs almost never hold one value at a time. They hold a basket of them — orders, messages, students, songs — and walk through the basket doing the same thing to each one. A list and a for loop are how Python does that, and it is a pairing you will reach for constantly.
Go straight to the code ↓A list is many values under one name
Every variable so far has held exactly one thing. One name, one score, one answer. A packing list is not like that. A packing list is one thing made of many things, kept in the order you wrote them down.
Python writes that with square brackets and a comma between each value:
items = ["Passport", "Charger"]That is a single variable called items holding two pieces of text. Passport is first, Charger is second,
and they will stay that way for as long as the program runs. Adding a third is a matter of typing a comma
and another value inside the same brackets. The brackets are what make it a list; the commas are what keep
the values apart.
A loop that hands you values, not numbers
This is the part worth slowing down on, because it is genuinely different from the loops so far.
for n in range(3) counts. It hands you 0, then 1, then 2, and every single one of them is a number.
Up to now that was what a loop was.
for item in items does not count at all. It walks along the list and hands you the value sitting there —
the text "Passport", then the text "Charger". Nothing is numbered, and you never say how many times to
go round, because the list already knows how long it is.
for n in range(3):
print(n)
for word in ["yes", "no", "maybe"]:
print(word)0
1
2
yes
no
maybeTwo loops that look almost the same and behave nothing alike. The name between for and in is yours to
choose either way — n, item, word, whatever reads best — and on every pass Python puts the next thing
into it for you.
len() says how many
len(items) gives back the number of values on the list, and len is short for length. It is worked out
fresh each time, so a list that grows reports a bigger number on its own.
A worked example
A shed rather than a suitcase, so the answer to this one stays yours to write:
colours = ["red", "green", "blue"]
for colour in colours:
print(f"Paint the shed {colour}")
print(f"{len(colours)} tins on the shelf")Paint the shed red
Paint the shed green
Paint the shed blue
3 tins on the shelfThree values on the list, three lines of output, then one line that counted them. Put a fourth colour in
the brackets and you get a fourth line and a 4 at the bottom, with nothing else edited.
Your turn
The editor holds a checklist with two items on it. Press Run before changing anything.
The loop is already doing its job: two items on the list, two lines of output. But both lines read
[ ] item, because the line inside the loop prints the word item in plain quotes rather than the value
the variable is holding. Quotes mean plain text, and plain text is exactly what came out.
Two things to do:
- Rewrite the line inside the loop so it prints the value instead, using an f-string like the one in the
worked example. Keep the
[ ]box and the space after it. Those square brackets are only a tick-box drawn in text — they sit inside quotes, so they make no list. - Add
"Socks"and"Towel"to the list, in that order, inside the square brackets.
The footer needs no attention at all. It is already counting the list, so it will say 4 by itself the
moment the two new items are in place. That is what len is for.
If something goes wrong
The likeliest slip is a missing f in front of the quote. You will spot it straight away, because the
output reads [ ] {item} word for word, braces and all, on every line. Nothing has broken; the quote
simply needs an f in front of it.
If one line reads [ ] PassportCharger and the footer is one short, a comma went missing between two items
inside the brackets. Python does not call that an error — it joins the two names into one value — so the
checklist itself is your only warning.
And if the program stops with TypeError: 'list' object cannot be interpreted as an integer, then range
has found its way into the for line. range wants a number to count up to, and a list is not a number.
Take it out; for item in items needs nothing else.
Write your code
Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.
Press Esc then Tab to move keyboard focus out of the code editor.
Output will appear here after you run your code.The runtime is starting in the background. You can type now — it will be ready before you are.
The answer appears here once you have run your code at least once.
Things that often go wrong here
- Writing for item in range(items)
- range counts numbers and needs a number to count up to, so Python stops with TypeError: 'list' object cannot be interpreted as an integer. There is nothing to count here. Writing for item in items hands you each value on the list directly, which is the whole reason this loop is shorter than a range loop.
- Leaving the f off the front of the quote
- Without the f the curly braces are not special, so every line prints the braces and the word between them exactly as typed, instead of the value the variable is holding, and all the lines still look identical. There is no error, because a piece of text with curly brackets in it is a perfectly legal thing to want.
- Forgetting the comma when adding an item to the list
- Two pieces of text sitting side by side inside the brackets with no comma between them do not produce an error. Python quietly joins them into one value, so the checklist shows a single line reading PassportCharger and the footer counts one item fewer than you expected. The output is the only warning you get.
- Typing the new total into the footer by hand
- The footer works out its own number from len(items), so it is already correct the moment the list grows. Replacing it with a typed number means the line stops telling the truth the next time an item is added or removed.
Longer explanation: read the full lesson. Want a blank editor instead? Open the Python playground.