The big idea: One variable holds one value. A list holds many, each reachable by its position.
That is what lets a loop handle a thousand items with the same three lines it uses for three.
Positions start at 0 here too: A list of four items has positions 0, 1, 2 and 3.
Asking for marks[4] is an error — and "off by one at the end" is the single commonest list bug.
Free preview
This is the free notes preview
You're reading the free notes. Aimnova Pro unlocks the full study experience — and you can try it with your first topic free to keep:
- FlashcardsLock in vocabulary and key terms with spaced repetition.
- Practice questionsAnswer exam-style questions and get instant AI marking.
- Mock exams & past-paper vaultSit full mocks and see exactly how examiners award marks.
- Personalised study planA daily plan built around your exam date and weak areas.
Adding
- append — at the end, the cheapest place
- insert(position, value) — anywhere, but everything after shifts along
- Java's ArrayList uses add
Removing
- remove(value) — the first matching item
- pop() — the last item, and gives it back to you
- pop(position) — a chosen position
- Everything after a removal shifts down
Traversing
- for item in list — when you only need the values
- an index loop over the positions — when you need the position, not just the value
- Never change a list's length while looping over it
Do not remove while you loop: Removing an item shifts everything after it down one place, while the loop's counter still moves up.
The result is that items get skipped, silently. Build a new list instead, or loop over a copy.
Stop wasting time on topics you know
Our AI identifies your weak areas and focuses your study time where it matters. No more overstudying easy topics.
A list of lists: A 2D list is a list whose items are themselves lists — a grid, with a row number and a column number.
Always in that order: [row][column].
Row first, always
A nested loop visits every cell
Rows and columns are not symmetric
How this is tested — you must get positions right and pick the correct traversal for the job. It comes up two ways:
Paper 2 — working with code
- Construct code using a 1D or 2D list, 4-6 marks
- Trace a loop over a list and state the output
- Add, remove or search within a list
Paper 2 — the algorithmic-thinking question
- Reason about a grid with no code shown
- Explain why a traversal visits what it does
The classic trap: Removing items while looping over the list. Everything after the removal shifts down while the counter moves up, so items are skipped silently. Build a new list instead.
A 2D list holds four students' marks in three subjects, one row per student. Write code to print each student's total and to find the highest single mark.
Model answer plan
See the mark-by-mark plan — for / against / judgement, with marking guidance — in study mode.