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

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 in it yet, so the first item will be joined onto nothing at all..

Watch out: animal and my_string are NOT the same thing. animal only ever holds ONE item, the current one. my_string is the one that keeps everything 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 actually hand 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

Here's the big one. planet IS the item; i is only where the item lives in the list.

Why the numbers start at 0

for planet in planets:

No numbers 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's 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 you a whole row: scores[0] is ["Ali", "Bea", "Cai", "Dev", "Eve"]. Two indexes get you one item. Commit to an answer before you read on.

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. Pick each missing step before you look at the next one.

  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 whoever got it. Instead it prints 18 18. Which line is the culprit?

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. 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

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 the result up 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, 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?
To store data in an organised and accessible way.
Array or list: what's the difference?
Array: static, and every item is the same data type. List: dynamic, and it can hold items of different data types. Both keep several items under one name.
What is an item's index?
Its position in the list. Lists use zero-based indexing, so the first item is at index 0.
Can an expression go inside the square brackets?
Yes, as long as it works out to an integer, e.g. planets[i] or planets[i + 1].
for item in my_list: what does item hold?
Each item in the list, one at a time, in order.
for i in range(len(my_list)): what does i hold?
The indexes 0, 1, 2 and so on, one per item. You fetch each item with my_list[i].
Why bother with the index form of the loop?
It gives you each item's position, so you can reach the item in the same position in another row.
How do you join every item of a list into one string?
Empty string before the loop; inside it, my_string = my_string + item.
A total has three parts. Where does each one go?
total = 0 before the loop, total = total + value inside it, print(total) after it.
You've got the total. How do you get the average?
Divide the total by the number of values.
What is a 2D list?
A list of lists: related items stored in rows and columns inside 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 that has 5 items per row?
for i in range(5): with 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 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 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.