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.
marks = [72, 65, 91, 48]
# 0 1 2 3 ← positions
print(marks[0]) # 72 — the first, at position 0
print(marks[-1]) # 48 — the last
print(len(marks)) # 4
marks[1] = 70 # change one place
print(marks) # [72, 70, 91, 48]int[] marks = {72, 65, 91, 48};
// 0 1 2 3 ← positions
System.out.println(marks[0]); // 72 — the first, at position 0
System.out.println(marks[marks.length - 1]); // 48 — the last
System.out.println(marks.length); // 4
marks[1] = 70; // change one place
System.out.println(Arrays.toString(marks)); // [72, 70, 91, 48]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.
Stopping an out-of-range index: Check an index before using it: if 0 <= i < len(marks) in Python, if (i >= 0 && i < marks.length) in Java.
Without the check, Python raises an IndexError and Java throws an ArrayIndexOutOfBoundsException, and the program stops.
Parallel arrays and arrays of objects: Two arrays can describe the same things position by position: names[3] and scores[3] belong to the same student. Sort or remove in one and the other must change the same way, or the pairs no longer match.
An array can also hold objects: points[2].getMemberId() calls the method of the object stored at index 2.
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.
names = ["Ana", "Ben"]
names.append("Cara") # add at the end
names.insert(0, "Dai") # add at a position
names.remove("Ben") # remove by value
last = names.pop() # remove the last and return it
print(names) # ['Dai', 'Ana']
# Traversing — visiting every item in turn
for name in names:
print(name)
# When you need the position as well:
for i in range(len(names)):
print(i, names[i])ArrayList<String> names = new ArrayList<>();
names.add("Ana");
names.add("Ben");
names.add("Cara"); // add at the end
names.add(0, "Dai"); // add at a position
names.remove("Ben"); // remove by value
String last = names.remove(names.size() - 1); // remove the last and return it
System.out.println(names); // [Dai, Ana]
// Traversing — visiting every item in turn
for (String name : names) {
System.out.println(name);
}
// When you need the position as well:
for (int i = 0; i < names.size(); i++) {
System.out.println(i + " " + names.get(i));
}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.
Older papers say "collection": Past papers before 2027 use the word collection for a list you add to and go through one item at a time. Today that is a Python list or a Java ArrayList: it changes size, keeps its items in order and has methods to add, remove and traverse.
Asked to draw an array? Draw one row of numbered boxes for each line of code, so every change is visible.
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].
grid = [[3, 8, 1], # row 0
[7, 2, 9], # row 1
[4, 6, 5]] # row 2
# c0 c1 c2
print(grid[1][2]) # 9 — row 1, column 2
print(grid[0]) # [3, 8, 1] — a whole row
# Visiting every cell: one loop per dimension
total = 0
for row in grid:
for value in row:
total = total + value
print(total) # 45int[][] grid = {{3, 8, 1}, // row 0
{7, 2, 9}, // row 1
{4, 6, 5}}; // row 2
// c0 c1 c2
System.out.println(grid[1][2]); // 9 — row 1, column 2
System.out.println(Arrays.toString(grid[0])); // [3, 8, 1] — a whole row
// Visiting every cell: one loop per dimension
int total = 0;
for (int[] row : grid) {
for (int value : row) {
total = total + value;
}
}
System.out.println(total); // 45Row first, always
grid[1][2] means row 1, column 2, giving 9.
Reversing them gives grid[2][1], which is 6 — a different cell, with no error to warn you.
A nested loop visits every cell
The outer loop takes each row; the inner loop takes each value in that row.
That is why scanning a grid is O(n²) — the work is rows times columns.
Rows and columns are not symmetric
A row is a real list, so grid[0] hands you one directly.
A column is not — reading column 2 means taking grid[r][2] from every row in turn.
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 answer broken into clear points, with guidance on how each point earns marks.