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 + animal
5 print(my_string)
animalmy_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.

1 / 6

Press Next one step at a time. Say what animal and my_string will hold BEFORE each step appears.

Step 1 of 6: my_string starts as an empty string. Nothing has been added yet, so the first item will be joined onto nothing at all..

Watch out: The loop variable and the growing string are different things. animal only ever holds ONE item, the current one. my_string is the one that keeps everything that has been added so far.

Two ways to visit every item

for planet in planets:vsfor i in range(len(planets)):

planets = ["Mercury", "Venus", "Earth", "Mars"]. Read across each row: what does each version give you?

Focus

What the loop variable holds each time

for planet in planets:

The item itself: "Mercury", then "Venus", then "Earth", then "Mars"

for i in range(len(planets)):

The index: 0, then 1, then 2, then 3. len(planets) is the length of the list, 4, so i takes one value per item

The insight

This is the big difference. planet IS the item; i is only its position in the list.

Why the numbers start at 0

for planet in planets:

No numbers are involved; you get the items directly

for i in range(len(planets)):

Lists use zero-based indexing, so the first item is at index 0

How you get the item

for planet in planets:

It is already in planet: print(planet)

for i in range(len(planets)):

Use i as the index: print(planets[i])

What gets printed

for planet in planets:

Mercury, Venus, Earth, Mars

for i in range(len(planets)):

Mercury, Venus, Earth, Mars

When to reach for it

for planet in planets:

When you only need each item

for i in range(len(planets)):

When you also need the position, for example to reach the item in the same position in another row

Two indexes

What does scores[0][2] hold?

A 2D list is a list of lists: related items stored in rows and columns, all inside one variable. scores = [["Ali", "Bea", "Cai", "Dev", "Eve"], [12, 15, 9, 18, 11]] Row 0 holds the names and row 1 holds each person's score. One index gets a whole row: scores[0] is ["Ali", "Bea", "Cai", "Dev", "Eve"]. Two indexes get one item.

Which is closest to what you think scores[0][2] holds?
How sure are you?

Totals and averages

Build the total, then the average

marks = [6, 9, 4, 8, 8]. Print the total of the marks, then find their average. After that, do the same for the scores in row 1 of the 2D list from the last block.

  1. The list: marks = [6, 9, 4, 8, 8]
  2. missing step
Which line is step 2?

Find the highest

Which line breaks the link?

This program should print the highest score and the name of the person who got it. Instead it prints 18 18. Which line is wrong?

A student's program — which line goes wrong?

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

1A for loop visits every item in a list, in order, once each.
2The loop variable holds only the current item. A separate variable collects the result.
3Indexes count from zero, so index 2 is the third item.
4Use the index form when you need an item's position, not just the item.
5Set up the result before the loop, update it inside, print it after.
6In a 2D list, the first index picks the row and the second picks the item in that row.

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?
Storing data in an organised and accessible way.
Array vs list: what's the difference?
An array is static and holds items of the same data type. A list is dynamic and can hold items of different data types. Both hold several items under one name.
What is an index?
An item's position in the list. Lists use zero-based indexing, so the first item is at index 0.
Can an expression be used as a list index?
Yes, as long as it works out to an integer, e.g. planets[i].
In for item in my_list, what does item hold?
Each item in the list, one at a time, in order.
In for i in range(len(my_list)), what does i hold?
The indexes 0, 1, 2 and so on. You fetch each item with my_list[i].
Why choose the index form of the loop?
It gives you each item's position, which lets you reach the item in the same position in another row.
How do you join every item of a list into one string?
Start with an empty string before the loop, then inside it: my_string = my_string + item.
Where do the three parts of a total go?
total = 0 before the loop, total = total + value inside it, print(total) after it.
How do you get the average once you have the total?
Divide the total by the number of values.
What is a 2D list?
A list of lists: related items stored in rows and columns in one variable.
scores[0] vs scores[0][2]?
scores[0] is the whole first row. scores[0][2] is the third item in the first row.
How do you loop along the second row of a 2D list of 5 items?
for i in range(5): and use scores[1][i]. The row index stays at 1 while i moves along.
Highest value: what do high_score and high_name start as?
high_score = 0 and high_name = "" (a blank string), both set before the loop.
Highest value: what happens inside the loop?
If the current score is larger than high_score, update high_score AND set high_name from the row above using the same index i.

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 posted

More Edexcel GCSE Computer Science topics

See the full Edexcel Computer Science curriculum →

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.