4.1 Declaring & Initialising a List
A list in Python is the practical version of an array in Cambridge pseudocode. You will use lists every time a question asks you to store a collection of items — names, marks, temperatures, queue contents — and process them one by one.
# A list of integers
numbers = [10, 20, 30, 40, 50]
# Mixed types are allowed (rare in Paper 4 but legal)
mixed = [5, "hello", 3.14, True]
# An empty list — items added later
empty = []- When pseudocode gives you
ARRAY[1:N], the safest Python equivalent ismyList = [0] * N(for integers) ormyList = ["" for i in range(N)](for strings). - This pre-fills the list so indexing never goes out of range.
# Fixed size of 10, pre-filled with zeros
scores = [0] * 10
# Fixed size of 20, pre-filled with empty strings
queueData = ["" for i in range(20)]- Paper 4 mark schemes reward visible structure.
- Always create your list on its own line before you use it — examiners want to see the declaration step exists separately from the processing step.
- One line per logical action.
- "Third" means index 2 (Python counts from 0)
- Lists are zero-indexed
- Lists are zero-indexed
- Fourth element is at index 3 (not 4)
Declaring & Initialising
4.2 Accessing & Modifying Items
Python indexing starts at 0 for the first item. Negative indices count from the end — -1 is the last item, -2 the second-to-last. Lists are mutable: you can replace, add or remove items.
| Item | 10 | 20 | 30 | 40 | 50 |
|---|---|---|---|---|---|
| Positive index | 0 | 1 | 2 | 3 | 4 |
| Negative index | -5 | -4 | -3 | -2 | -1 |
Reading & changing
numbers = [10, 20, 30, 40, 50]
print(numbers[0]) # 10 → first
print(numbers[2]) # 30 → third
print(numbers[-1]) # 50 → last
print(numbers[-2]) # 40 → second-to-last
# Replace an item (lists are mutable)
numbers[1] = 25
print(numbers) # [10, 25, 30, 40, 50]Adding & removing
fruits = ["apple", "banana"]
fruits.append("mango") # end → ["apple","banana","mango"]
fruits.insert(1, "orange") # idx 1 → ["apple","orange","banana","mango"]
fruits.remove("orange") # first match
last = fruits.pop() # remove+return last (stack pop)
print(len(fruits)) # number of items- If a list has
nitems, the valid indices are0ton − 1. - So
len(marks)is 5, butmarks[5]is an error. - This off-by-one mistake is the most common cause of
IndexErrorcrashes in Paper 4.
- When a question says "the
nth item", always convert it in your head to "indexn − 1" before you write code. - Writing the wrong index is the single most common Paper 4 list error and almost never earns the mark.
- Lists are zero-indexed
- Fifth item is at index 4
Access & Modify
4.3 Traversing a List
Almost every Paper 4 list question needs you to visit every element. That visiting process is called traversal, and there are exactly two ways to do it in Python — you need both.
Form 1 — Direct loop (values only)
numbers = [10, 20, 30, 40, 50]
for num in numbers:
print(num)
# num is each list item in turn.
# Use when you do NOT need the index.Form 2 — Index loop (positions / modify)
numbers = [10, 20, 30, 40, 50]
for i in range(len(numbers)):
print("Item at index", i, "is", numbers[i])
# range(len(...)) gives 0, 1, 2, 3, 4 —
# every valid index. Use to modify items
# (numbers[i] = ...) or report positions.- Pseudocode often uses bounds
1 TO N; Python'srange(N)produces0 TO N − 1. - Translate the algorithm, not the bounds — the logic stays the same, only the starting index differs.
- Examiners give marks for the right kind of loop, not just any loop.
- If the question implies positions ("which student", "at what index"), use the index form — even if the direct form also produces the right output.
- Picking the form that matches the question shows you understand why, not just how.
- Day numbering starts at 1
- List index starts at 0
- Adjust by adding 1 to the index
- Day numbering starts at 1
- List index starts at 0
- Adjust by adding 1 to the index
Traversing a List
4.4 Counting & Totalling
"How many?" and "what is the total?" are the two most common Paper 4 sub-questions. Both use the accumulator pattern: a variable that starts at zero before the loop, gets updated inside the loop, and is printed after the loop.
- If you initialise inside the loop, the value resets every iteration (wrong answer).
- If you output inside the loop, you print every partial result (wrong output).
- Order matters.
Totalling a list
marks = [56, 78, 45, 90, 33]
total = 0 # BEFORE
for m in marks:
total = total + m # INSIDE
print("Total:", total) # AFTERConditional counting (passes)
marks = [56, 78, 45, 90, 33]
passes = 0
for m in marks:
if m >= 50:
passes = passes + 1
print("Passes:", passes)- Cambridge mark schemes typically award a mark for the correct initialisation, a mark for the correct update inside the loop, and a mark for the output after the loop.
- That is three marks just for following the pattern — skipping the init line loses a free mark.
- Use one loop with two accumulators
- Initialise both accumulators before the loop starts
- Update both inside the loop body
- Use one loop with two accumulators
- Initialise both accumulators before the loop starts
- Update both inside the loop body
Counting & Totalling
4.5 Maximum, Minimum & Average
Finding the highest mark, the coldest temperature, or the average speed are stock Paper 4 questions. To find a max or min you cannot start the tracker at 0 — if all the values are negative, the answer would wrongly come out as 0. Start at the first element instead.
- Starting at
list[0]guarantees the tracker holds a real value from the list. - Starting at
0assumes data you do not have. - The exam will sometimes give negative values, decimals, or all-equal lists to catch students who started at 0.
# Max, min AND average in ONE traversal
values = [12, 45, 7, 89, 3, 22]
maxValue = values[0] # start with the first element
minValue = values[0]
total = 0
for v in values:
if v > maxValue:
maxValue = v
if v < minValue:
minValue = v
total = total + v
average = total / len(values)
print("Max:", maxValue)
print("Min:", minValue)
print("Average:", round(average, 2))- Do not use Python's built-in
max(),min(), orsum()when the question says "write an algorithm to find…". - The mark scheme awards marks for the comparison logic inside the loop.
- Built-ins skip the logic and earn zero method marks.
- Initialise maxT and minT using temps[0] (not 0)
- Initialise total using 0
- Update all three inside the loop
- Initialise maxOut and minOut using output[0] (not 0)
- Initialise total using 0
- Update all three inside the loop
Max, Min & Average
4.6 Linear Search & Frequency
Linear search is a guaranteed Paper 4 question type. The 9618 syllabus says you must write the algorithm — you cannot just use Python's in operator.
- When a 9618 question asks you to implement a linear search, you must manually loop through the list and compare each element.
- Using
in,index(), orcount()will not earn the method marks.
The found-flag pattern
items = [3, 8, 6, 12, 9]
target = 6
found = False # assume not found
position = -1 # sentinel: "no valid position"
for i in range(len(items)):
if items[i] == target:
found = True
position = i
break # stop on first match
if found:
print("Found at index", position)
else:
print("Not found")foundstartsFalse— we assume not found until proven otherwise.position = -1is a sentinel meaning "no valid position" (list indices are never negative).breakexits as soon as the target is found — without it, a duplicate target would overwriteposition.
- Even when the target is guaranteed to exist, still write the
elsebranch. - The mark scheme awards an explicit mark for handling the "not found" case.
- Skipping it loses that mark.
Frequency count (conditional counter)
nums = [4, 1, 4, 6, 4, 7, 2]
target = 4
count = 0
for n in nums:
if n == target:
count = count + 1
print("Frequency of", target, ":", count)
# Output: Frequency of 4 : 3- Assume not found (flag = False) before the loop
- Update the flag and the position inside the if block
- Use break to stop on the first match
- Assume not found (flag = False) before the loop
- Update the flag and the position inside the if block
- Use break to stop on the first match
Linear Search & Frequency
4.7 Removing Duplicates (Unique List)
Building a duplicate-free copy of a list is the first algorithm on this page that uses a nested loop: an outer loop over the original list, and an inner loop over the unique list being built.
original = [1, 2, 2, 3, 4, 4, 1]
unique = []
for item in original:
# Manual "is it already there?" check
alreadyIn = False
for u in unique:
if u == item:
alreadyIn = True
break
if not alreadyIn:
unique.append(item)
print("Unique:", unique)
# Output: Unique: [1, 2, 3, 4]- Examiners expect to see the nested loop here.
- The shortcut
if item not in uniquewould work but uses theinoperator — avoid it when the question requires you to write the search yourself.
- When the question says "produce a list of unique values", the mark scheme requires a new list, not a modified original.
- Modifying the original list while looping through it causes index errors and is almost always penalised.
- Use the same two-step structure
- Step 1: one accumulator for the count
- Step 2: a nested loop to build the unique list
- Use the same two-step structure
- Step 1: one accumulator for the count
- Step 2: a nested loop to build the unique list
4.8 Swapping Elements
Every sorting algorithm in Paper 4 — bubble sort, insertion sort — is built on top of swapping two elements. To swap safely you need a temporary variable to hold one value while the other is overwritten.
values = [1, 2, 3, 4, 5]
temp = values[0] # 1. SAVE the first value
values[0] = values[-1] # 2. OVERWRITE first with last
values[-1] = temp # 3. RESTORE saved value into last
print(values) # [5, 2, 3, 4, 1]- Doing
values[0] = values[-1]first destroys the original value at index 0. - If you then write
values[-1] = values[0], both elements end up the same. - The temp variable stores the soon-to-be-destroyed value so it can be put back in the right place.
- Python lets you swap on one line with tuple unpacking —
values[0], values[-1] = values[-1], values[0]— but Paper 4 mark schemes traditionally expect the three-step version because it mirrors the Cambridge pseudocode patternPROCEDURE SWAP(BYREF X, Y). - Use the three-step form unless the question explicitly allows Python idioms.
- The three swap lines are usually awarded one mark each — and the order matters.
- Writing them in the wrong order loses the mark even if the code happens to work for some inputs.
- Make the order automatic: save, overwrite, restore.
- Save the first value to a temp variable
- Overwrite first with the last value
- Restore the saved value using temp
- Save the first value to a temp variable
- Overwrite first with the last value
- Restore the saved value using temp
4.9 Mixed Paper 4 Tasks
These tasks combine the skills from 4.1–4.8 into compound questions you will meet in the real Paper 4.
- Use three accumulators
- All start at 0
- All updated inside one loop
- Keep two accumulators: total of qualifying ages and a count
- Only add when the age qualifies
- Divide total by count at the end
- Use a conditional accumulator
- Only add when the value passes the check
- Initialise the accumulator to 0 before the loop
- Use % 2 to find the remainder
- Remainder 0 means the number is even
- Otherwise the number is odd
- Almost every Paper 4 list question rewards the same structure: initialise variables → declare/receive the list → write the loop → update inside the loop → output after the loop.
- Make this your default mental template.
- Identify the type of accumulator (count, sum, max/min, search flag, swap-temp) first; then drop the right pattern into the template.
4.10 Practice Tasks
Fifteen exam-style tasks. Click Hint for bullet-point guidance, then Help to reveal a worked Python solution.
✓ Key Points Summary
Question Bank
Answer all questions, then press Submit Quiz to see your score.
Question 1Multiple Choice
Which line creates a Python list of 5 integers?
Question 2True / False
In nums = [10, 20, 30, 40, 50], nums[5] returns the last item (50).
Question 3Multiple Choice
What does append(x) do?
Question 4Multiple Choice
Which loop form is correct when you need to MODIFY list items by index?
Question 5True / False
An accumulator (like total) must be initialised BEFORE the loop and printed AFTER the loop.
Question 6Multiple Choice
Why do max/min trackers start at list[0] rather than 0?
Question 7Multiple Choice
In a linear search, what is the starting value of the `found` flag?
Question 8True / False
Using Python's built-in max() earns full marks when the question says "write an algorithm to find the maximum".
Question 9Multiple Choice
What is the output? nums = [4, 1, 4, 6, 4, 7, 2] count = 0 for n in nums: if n == 4: count += 1 print(count)
Question 10Multiple Choice
To swap two list elements safely, you need a temporary variable. What is the correct 3-step order?
Question 11True / False
When building a unique-values list, you should modify the original list while looping through it.
Question 12Multiple Choice
Which is the standard 5-step template for almost every Paper 4 list question?
Answer all 12 questions to enable submission.