GCSE · Computer Science · Edexcel · Spec 1CP2

Programming – iteration over data structures

Four animals, one loop, and a string that builds itself: cat, then catdog, then catdogmouse, then catdogmousegoat. Press Start and watch it happen.

Computer Science · Algorithms

Trace table — dry run the code

Before you press Next step on each pass, say what result will be. Then check.

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?

Sort each property. Some belong to arrays only, some to lists only, and some to 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 forms print every planet. The difference is what the loop variable holds.

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

This is the whole difference. planet is the thing; i is the position of the thing.

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

A string can be looped over just like a list. Work out which positions are 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. Think of it as two rows and three columns.

What does scores[0] give 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 are looping over.
  2. evens = []An empty list to collect into. It is 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 items of one data type. A list can grow or shrink and can mix data types.
  • Every item sits at an index, its position. Counting starts at 0, so in planets = ["Mercury", "Venus", …], planets[0] is Mercury.
  • Have a goIn planets = ["Mercury", "Venus", …], what does planets[1] give you? Say it before you look.

    Venus

    Counting starts at 0, so index 0 is Mercury and index 1 is the second item, Venus.

  • A for loop can give you each item itself: for planet in planets assigns planet to each item in turn.
  • Or it can give you each index: for i in range(len(planets)) makes i 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 of the loop?

    0

    i is an index, not a planet, and indexes start at 0. The item you want is planets[i].

  • A loop can build a result. Start with an empty string, join each item on, and you get cat, catdog, catdogmouse, catdogmousegoat.
  • Numbers work the same way. Start a total at 0, add each value, then divide by the number of values to get 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 the total be?

    Sam gets 6; it should be 5.

    The loop adds onto whatever total starts as, so a start of 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)) works for a word of any length.
  • A two-dimensional list is a list of lists, indexed from 0 on rows and columns. scores[0] is a whole row.
  • scores[0][2] is the single item at row 0, column 2, and scores[row][i] lets a loop work along one row.
  • 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. The row comes first, then the column.

  • Because lists are dynamic, a loop can add items as it runs with the append method, which puts an item on the end.

The big picture

A for loop can visit every item in a list, or every character in a string, either by the item itself or by 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.
2Item loop: the variable holds each item. Index loop: the variable holds 0, 1, 2, … and you use list[i].
3To build a result, initialise it first (an empty string, or a total of 0), then update it on every pass.
4In a two-dimensional list, scores[0] is a row and scores[0][2] is one item: row first, then column.
5append adds an item to the end of a list 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 people out. One: thinking the first item is at index 1 (it is at index 0). Two: reading scores[0] in a two-dimensional list as a single value (it is a whole row, and scores[0][2] is one item).

🧠

Memory hook

Picture numbered shelves, starting at shelf 0. A loop can grab each thing as it passes, or read each shelf number and then look. Same shelves, same things.

✓

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

(12)
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, indexed from 0 on both.
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 9 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.