GCSE · Computer Science · Edexcel · Spec 1CP2

Programming – iteration over data structures

Four animals. One loop. A string that builds itself: cat, then catdog, then catdogmouse, then catdogmousegoat. Step through it below and call out each value before it appears.

Computer Science · Algorithms

Trace table — dry run the code

Before each Next step, say out loud what result will hold. Then check if you were right.

1animals = ["cat", "dog", "mouse", "goat"]
2result = ""
3for animal in animals:
4 result = result + animal
5 print(result)
animal
result
Output
Press Start to run the first line.
·

Ready when you are — step through one line at a time.

Computer Science · Data structures

Array, list, or both?

Drop each property where it belongs. Some are arrays only, some are lists only, and some are true of both.

  • A Array
  • B List
  1. Fixed size
  2. All items must be the same data type
  3. Can grow or shrink
  4. Can contain items of different data types
  5. Stores data in an organised, accessible way
  6. Each item is referred to by a zero-based index

Two ways to loop over a list

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

Both loops print every planet. Look at what the loop variable is holding each time.

Focus

What the loop variable holds

for planet in planets:

Each item itself, in turn: first "Mercury", then "Venus", and so on.

for i in range(len(planets)):

An index each time: 0, then 1, then 2, and so on.

The insight

That's the whole difference. planet is the thing. i is where the thing lives.

How you get the item to print

for planet in planets:

print(planet)

for i in range(len(planets)):

print(planets[i])

What comes out

for planet in planets:

Every item in the list, in turn.

for i in range(len(planets)):

Every item in the list, in turn.

Predict, then check

Strings can be looped over just like lists. Work out which positions get visited first.

my_word = "sheep" for i in range(0, 5, 2): print(my_word[i]) What does this print?

Computer Science · Two-dimensional lists

What does one index give you?

scores = [[4, 7, 9], [6, 2, 8]] — a list of lists. Picture it as two rows and three columns.

Be honest: what do you think scores[0] gives you?
How sure are you?

Computer Science · Lists are dynamic

Collect the even numbers

Write a loop that goes through numbers and builds a new list containing only the even numbers.

  1. numbers = [3, 8, 5, 6, 2]The list we're looping over.
  2. evens = []An empty list to collect into. It's the list version of starting result as an empty string.
  3. for number in numbers:The loop visits each item in turn.
  4. missing step
Which line is step 4?

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

What you need to know

  • An array has a fixed size and holds one data type. A list can grow or shrink and can mix data types.
  • In Python, a list can be used as an array if every element has the same type and nothing is added or deleted.
  • Every item sits at an index, its position. Counting starts at 0, so planets[0] is Mercury and planets[1] is Venus.
  • Have a goplanets = ["Mercury", "Venus", …]. Before you look: what does planets[1] give you?

    Venus

    Counting starts at 0, so planets[1] is the second item. Answering Mercury means you counted from 1.

  • for planet in planets hands the loop each item itself, one after another, until the list runs out.
  • for i in range(len(planets)) hands the loop each index instead: 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: "Mercury", 0 or 1?

    0

    i holds an index, not a planet, and indexes start at 0. The planet itself is planets[i].

  • To build a string, start with an empty one and join each item on: cat, catdog, catdogmouse, catdogmousegoat.
  • For numbers, start a total at 0 and add each value. Total divided by the number of values gives 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 it be?

    Sam gets 6. It should be 5.

    The loop adds onto whatever total starts as, so starting at 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)) fits any length.
  • A two-dimensional list is a list of lists, counted from 0 both ways: scores[0] is a whole row, scores[0][2] one item.
  • 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. Row first, then column, both from 0.

  • Lists are dynamic, so a loop can add items as it runs with the append method, which puts each one on the end.

The big picture

A for loop can visit every item in a list, or every character in a string, either by handing you the item itself or by handing you 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

1Arrays are fixed-size and single-type; lists are dynamic and can mix types. Both are indexed from 0.
2A Python list can stand in for an array when every element has the same type and nothing is added or deleted.
3Item loop: the variable holds each item. Index loop: the variable holds 0, 1, 2, … and you use list[i].
4To build a result, set it up first (an empty string, or a total of 0), then update it on every pass.
5In a two-dimensional list, scores[0] is a row and scores[0][2] is one item: row first, then column.
6append adds an item to the end of a list, even while a loop is running.

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 loads of people. One: thinking the first item is at index 1. It's at index 0. Two: reading scores[0] in a two-dimensional list as a single value. It's a whole row; scores[0][2] is one item.

🧠

Memory hook

Picture a row of shelves numbered from 0. A loop can grab each thing as it walks past, or read each shelf number and then look. Same shelves, same things, two ways of walking.

✓

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

(13)
What is an array?
A static data structure of fixed size that contains items of the same data type.
What is a list?
A dynamic data structure that can grow or shrink and can contain items of different data types.
When can a Python list be used as an array?
When every element has the same data type and no items are added or deleted.
What is an index, and where does it start?
The position of an item in a list. Lists use zero-based indexing, so the first item is at index 0.
What does the loop variable hold in for planet in planets?
Each item in the list, in turn.
What does the loop variable hold in for i in range(len(planets))?
An index: 0, 1, 2, … The item is then planets[i]. Both loops output every item in turn.
Why start result as an empty string before a concatenating loop?
The loop joins each item onto result, so it needs a starting value that adds nothing: the empty string.
How do you total and average a list of numbers with a loop?
Start total at 0, add each value in the loop, then divide the total by the number of values.
How do you loop over the characters of a string?
for letter in my_word assigns each character in turn. An index loop uses my_word[i].
Why use range(len(my_word)) in an index loop?
It makes the loop work for a string of any length.
What is a two-dimensional list?
A list of lists that stores related items in rows and columns within one variable.
In a two-dimensional list, what is the difference between scores[0] and scores[0][2]?
scores[0] is the whole of row 0. scores[0][2] is the single item at row 0, column 2.
What does append do?
It adds an item to the end of a list, which is possible because lists are dynamic.

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