GCSE · Computer Science · Edexcel · Spec 1CP2
Iteration over a data structure
Four animals or four hundred: one short for loop can visit every item in a list and do the same job with each.
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 is 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 has been added 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. An array is static and holds items of one data type; a list is dynamic and can hold different data types. Both keep several items under one name.
- An index is an item's position. Lists use zero-based indexing, so the first item is planets[0] and planets[1] is the second. Any expression that works out to an integer can be used as an index.
- for item in list gives you each item in turn. for i in range(len(list)) gives you each index (0, 1, 2 and so on), and list[i] fetches the item. Both give the same output.
- To build a result, set it up BEFORE the loop (an empty string, or a total of 0), update it INSIDE the loop, and print it AFTER the loop. The average is the total divided by the number of values.
- A 2D list is a list of lists: rows and columns in one variable. scores[0] is a whole row; scores[0][2] is row 0, item 2. Both indexes start at zero.
- To find the highest value, start high_score at 0 and high_name blank. When the current score is larger than high_score, update both, using the same index i to fetch the name from the row above.
The big picture
A list stores several items under one name, and a for loop can visit every one of them in turn. You can loop through the items themselves (for planet in planets) or through their indexes (for i in range(len(planets))), which count from zero. The same loop can join items into a string, add them to a total to find an average, or keep track of the highest value, and it works on one row of a 2D list by fixing 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. In a 2D list both indexes count from zero, so scores[1] is the second row, not the first.
Memory hook
Set it up, add it in, print it out. And in a 2D list, think "row, then item, both from zero".
Check yourself
Explain out loud: why does my_string start as "" and total at 0, and why print them after the loop? Then say which row and item scores[1][4] points to.
Flashcards
(15)What is a data structure for?
Array vs list: what's the difference?
What is an index?
Can an expression be used as a list index?
In for item in my_list, what does item hold?
In for i in range(len(my_list)), what does i hold?
Why choose the index form of the loop?
How do you join every item of a list into one string?
Where do the three parts of a total go?
How do you get the average once you have the total?
What is a 2D list?
scores[0] vs scores[0][2]?
How do you loop along the second row of a 2D list of 5 items?
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 29 September 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.