GCSE · Computer Science · Edexcel · Spec 1CP2
Iteration over a data structure
Four animals or four hundred, it makes no difference. One short for loop visits every item in a list and does the same job with each one. Watch.
Iteration · Watch it happen
One loop, four animals, one growing string
A list keeps several items under one name, so one short loop can visit them all. (An array does the same job, but it's static and every item has the same data type; a list is dynamic and can mix data types.)
Before you press Next: what will my_string hold after the second pass?
1 animals = ["cat", "dog", "mouse", "goat"]2 my_string = ""3 for animal in animals:4 my_string = my_string + animal5 print(my_string)
| animal | my_string |
|---|---|
| "" |
Output
Step 1: my_string starts as an empty string. Nothing in it yet, so the first item will be joined onto nothing at all.
Press Next one step at a time. Say what animal and my_string will hold BEFORE each step appears.
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
One loop, every item: build a string, add up a total, find the highest.
What you need to know
- Data structures store data in an organised, accessible way. Arrays and lists both keep several items under one name.
- An array is static and every item is the same data type; a list is dynamic and can mix data types.
- An index is an item's position, counted from zero: planets[0] is the first item, planets[1] the second.
- Any expression can go in the square brackets, like planets[i + 1], as long as it works out to an integer.
Have a gopets = ["cat", "dog", "fish"] and n = 2. What does pets[n - 1] give you?
"dog"
n - 1 works out to 1, and index 1 is the second item. "cat" is what you get if you count from one instead of zero.
- for planet in planets hands you each item in turn. for i in range(len(planets)) hands you each index: 0, 1, 2 and so on.
- In the index version, planets[i] fetches the item. Both loops give exactly the same output.
- Building a result? Set it up before the loop (an empty string, or a total of 0), update it inside, print it after.
Have a goYour mate Jas moves my_string = "" INSIDE the loop, just above the line that adds each item. "Same thing, right?" They loop over ["cat", "dog"]. What does print(my_string) show after the loop?
dog
The reset now runs on every pass, wiping "cat" before "dog" is added. Only setting it up before the loop lets the string keep growing to catdog.
- The average is the finished total divided by the number of values.
- A 2D list is a list of lists. scores[0] is a whole row; scores[0][2] is row 0, item 2. Both count from zero.
Have a gogrid = [[5, 8], [2, 9]]. Before you peek: what is grid[1][0]?
2
Row first: row 1 is the second row, [2, 9]. Then item 0 is the first thing in it. 8 is what you get if you read the indexes the wrong way round.
- Highest value: start high_score at 0 and high_name blank. When a score beats high_score, update both, using the same i.
The big picture
A list keeps lots of items under one name, and a for loop can visit every one of them in turn. You can loop over the items themselves (for planet in planets) or over their indexes (for i in range(len(planets))), which count from zero. Same loop shape, different jobs: join items into a string, add them up for a total and an average, or keep track of the highest value. It even works along one row of a 2D list: just fix the row index.
Key points
Worked example
Problem
A 2D list stores a team's names and goals: team = [["Kai", "Zoe", "Sam"], [3, 7, 5]]. Write a loop that joins all the names into one string, then say what it prints.
⚠ Watch out
Counting from one. The first item in a list is at index 0, so planets[1] is the SECOND item, not the first. Starting at 1 feels natural, which is exactly why it catches people. In a 2D list both indexes count from zero, so scores[1] is the second row.
Memory hook
Set it up, add it in, print it out. And for a 2D list: "row, then item, both from zero".
Check yourself
Say it out loud: why does my_string start as "" and total start at 0, and why print them after the loop? Then: which row and which item does scores[1][4] point to?
Flashcards
(15)What is a data structure for?
Array or list: what's the difference?
What is an item's index?
Can an expression go inside the square brackets?
for item in my_list: what does item hold?
for i in range(len(my_list)): what does i hold?
Why bother with the index form of the loop?
How do you join every item of a list into one string?
A total has three parts. Where does each one go?
You've got the total. How do you get the average?
What is a 2D list?
scores[0] vs scores[0][2]?
How do you loop along the second row of a 2D list that has 5 items per row?
Highest value: what do high_score and high_name start as?
Highest value: what happens inside the loop?
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 28 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.