Lesson 8 of 16 · 52 min · UG / professional
Lists: index, append, nest, mutate
A day’s UPI rows is a numbered tray. You can replace the third amount. You can put a list inside a list for a 2×2 lab grid. Index 0 is still the first cell.
Market shopping list
A list holds many items together.
You will be able to
- ✓ Index from 0, append, len
- ✓ Replace an item by index
- ✓ Nest lists as a small grid
A numbered tray that may grow
A day’s UPI rows is a list. Index 0 is the first settlement. append is the evening reversal. len is how many tickets remain. Unlike strings, you may write rows[2] = 15 to fix a wrong amount — lists are mutable.
Concatenation on a laptop (a + b) builds a new list. Nesting [row0, row1] is a 2-row grid; x[0][1] is a laptop spelling — here keep two named lists or a list of dicts so keys stay labelled.
Bounds and copies
An index past the end is an error on a laptop. A slice copy squares[:] is a shallow copy. If you iterate a list and insert into it at the same time, you can loop forever — iterate a snapshot (you already practised copying).
Words that matter
- Mutable
- May change in place — lists can; strings cannot.
- append
- Add one item at the end.
- Nested list
- A list whose items are lists — a grid.
Common mistakes
Avoid: Calling the first row rows[1] because humans count from one.
Do this: rows[0] is the first.
Run it step by step
Each box is a real program. Press Run, change a number, Run again — the output must follow your code.
1. Step 1 — first item, count, append
Grow the tray. Count follows.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
2. Step 2 — fix a cell by index
The third amount was wrong. Replace it.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Example program — Walk and total
A loop visits; the source stays.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Your turn — Add the late row
Start with [10, 20]. Append 30. Print so 30 and a count of 3 both appear.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Self-assessment
Check your understanding before you mark the lesson complete.
Progress is stored in a browser cookie on this device.