Community resourceWorksheet
J277 2.2.3 Processing arrays with loops
Part 3 of 5 · J277 2.2.3 · Strings, arrays and functions
The loop patterns that appear in almost every OCR array question.
Students will:
- total and average the values in a list
- count values that meet a condition
- find a maximum or minimum correctly
- update every element with a loop
- debug a faulty minimum-finding algorithm
Inside: 6 explanation cells, 4 runnable Python tasks, 3 multiple-choice questions and 2 written answers. 18 marks, about 45 minutes.
Series: J277 2.2.3 · Strings, arrays and functions, part 3 of 5.
Shared by Coding PathwayVerified teacher
- 15 cells
- About 45 minutes
- CC BY-SA 4.0
- Shared 17 Aug 2026
- Updated 9 Sept 2026
Preview
The whole resource, exactly as a class sees it. Answers and marking are held back.
Processing arrays with loops
Arrays become powerful when a loop processes each element. Common exam tasks include calculating a total or average, counting values that meet a condition, finding an extreme value and updating elements.
In Python, for score in scores: assigns each element to score in turn. The indented statements then process that current element before the loop moves to the next one.
Which loop header processes every value in scores directly?
- Aif score in scores:
- Bwhile scores = score:
- Cfor score in scores:
- Dprint(scores):
Common patterns
Initialise an accumulator or counter before the loop. For a maximum, a safe pattern is to initialise from the first element rather than guessing a starting value.
highest = scores[0]
for score in scores:
if score > highest:
highest = score
This works even if every score is negative.
Be clear whether a loop variable stores an element or an index. In for score in scores:, score is the current value, so writing scores[score] would incorrectly treat that value as a position. When positions are required, for example to use matching positions in two arrays, loop through indices instead: for index in range(len(scores)):.
The example below combines an accumulator, a counter and an average. Before running it, predict total and above_twelve. Notice that the average is calculated after the loop, when the complete total is available.
readings = [12, 15, 9, 17, 11]
total = 0
above_twelve = 0
for reading in readings:
total = total + reading
if reading > 12:
above_twelve = above_twelve + 1
average = total / len(readings)
print(total, average, above_twelve)
In for reading in readings:, what does reading contain on each repetition?
- AThe length of the list
- BThe index of the current element
- CThe whole list
- DThe current element's value
Supported practice: total and average
Process every value in scores using a loop. Add each score to the running total, then calculate the average after the loop using total / len(scores).
total = 0 is required initialisation for the accumulator. The expected total is 75 and the expected average is 15.0.
# Calculate the total and average score.
scores = [18, 12, 15, 20, 10]
total = 0
# Add a loop and calculate average.
Why can highest = 0 be an unsafe initial value when searching a list for its highest element?
- AAll actual values might be negative
- BZero cannot be stored
- CThe loop will always be infinite
- DLists cannot contain numbers
Debugging task: find the true minimum
The supplied temperatures are all negative, so the initial statement lowest = 0 is unsafe: zero is not an element in the data and would hide some incorrect comparison logic.
Initialise lowest from the first array element, loop through all temperatures and replace lowest only when a smaller value is found. The final result should be -9.
# Find the lowest temperature without assuming it is positive or negative.
temperatures = [-4, -9, -2, -7]
lowest = 0
# Correct the initial value and use a loop with selection.
Independent practice: count values in an inclusive range
Count the stored scores from 40 to 60 inclusive. Loop through every value, combine both boundary comparisons with and, and increase in_range only when the condition is true.
Boundary values 40 and 60 must both be counted. The supplied list contains four qualifying scores.
# Count scores from 40 to 60 inclusive.
scores = [39, 40, 52, 60, 61, 47]
in_range = 0
# Use a loop and a compound condition.
Explain why total = 0 must be placed before, rather than inside, a loop that calculates the sum of an array.
Compare retaining the running total with resetting it on each repetition.
Students type their answer here.
Design an algorithm to count how many names in an array have more than six characters. State the initialisation, loop, condition and update.
Use `len(name)` in your explanation or Python-style algorithm.
Students type their answer here.
Review
When processing an array, initialise once before the loop, visit every required element and update only when the rule says to. Initialise a minimum or maximum from real array data, and calculate an average only after the complete total is known.