GCSE · Computer Science · Edexcel · Spec 1CP2
Programming – iteration over data structures
Four animals, one loop, and a string that builds itself: cat, then catdog, then catdogmouse, then catdogmousegoat. Press Start and watch it happen.
Computer Science · Algorithms
Trace table — dry run the code
Before you press Next step on each pass, say what result will be. Then check.
Ready when you are — step through one line at a time.
Predict, then check
A string can be looped over just like a list. Work out which positions are visited first.
my_word = "sheep" for i in range(0, 5, 2): print(my_word[i]) What does this print?
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
What you need to know
- An array has a fixed size and holds items of one data type. A list can grow or shrink and can mix data types.
- Every item sits at an index, its position. Counting starts at 0, so in planets = ["Mercury", "Venus", …], planets[0] is Mercury.
Have a goIn planets = ["Mercury", "Venus", …], what does planets[1] give you? Say it before you look.
Venus
Counting starts at 0, so index 0 is Mercury and index 1 is the second item, Venus.
- A for loop can give you each item itself: for planet in planets assigns planet to each item in turn.
- Or it can give you each index: for i in range(len(planets)) makes i 0, 1, 2, … and you print planets[i].
Have a goIn for i in range(len(planets)), what is i on the very first pass of the loop?
0
i is an index, not a planet, and indexes start at 0. The item you want is planets[i].
- A loop can build a result. Start with an empty string, join each item on, and you get cat, catdog, catdogmouse, catdogmousegoat.
- Numbers work the same way. Start a total at 0, add each value, then divide by the number of values to get the average.
Have a goSam starts total = 1 'so it isn't empty', then loops over [2, 3] adding each value. What does Sam get, and what should the total be?
Sam gets 6; it should be 5.
The loop adds onto whatever total starts as, so a start of 1 sneaks an extra 1 into the answer. A total starts at 0.
- Strings loop like lists: for letter in my_word gives each character, and range(len(my_word)) works for a word of any length.
- A two-dimensional list is a list of lists, indexed from 0 on rows and columns. scores[0] is a whole row.
- scores[0][2] is the single item at row 0, column 2, and scores[row][i] lets a loop work along one row.
Have a goscores = [[4, 7, 9], [6, 2, 8]]. What is scores[1][0]?
6
scores[1] is the second row, [6, 2, 8], and index 0 picks its first item. The row comes first, then the column.
- Because lists are dynamic, a loop can add items as it runs with the append method, which puts an item on the end.
The big picture
A for loop can visit every item in a list, or every character in a string, either by the item itself or by its index. Each pass can add to a result. Lists and arrays count from 0, and a two-dimensional list needs a row index and then a column index.
Key points
Worked example
Problem
scores = [6, 9, 4, 5]. Use a loop to find the total of the scores, then their average.
⚠ Watch out
Two slips catch people out. One: thinking the first item is at index 1 (it is at index 0). Two: reading scores[0] in a two-dimensional list as a single value (it is a whole row, and scores[0][2] is one item).
Memory hook
Picture numbered shelves, starting at shelf 0. A loop can grab each thing as it passes, or read each shelf number and then look. Same shelves, same things.
Check yourself
Cover the page. numbers = [3, 8, 5], total starts at 0 and a loop adds each number on. What is total after each pass, and why does it start at 0?
Flashcards
(12)What is an array?
What is a list?
When can a Python list be used as an array?
What is an index, and where does it start?
What does the loop variable hold in for planet in planets?
What does the loop variable hold in for i in range(len(planets))?
Why start result as an empty string before a concatenating loop?
How do you total and average a list of numbers with a loop?
How do you loop over the characters of a string?
Why use range(len(my_word)) in an index loop?
What is a two-dimensional list?
What does append do?
Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.
Learning with Lightbulb is opening soon
You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.
Keep me postedMore Edexcel GCSE Computer Science topics
How this lesson was checked. This Edexcel GCSE Computer Science (specification 1CP2)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 9 October 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.