← VCE Software Development U3O1

Looping through a list with for...in

Runs the indented block once for each value in the list. The variable item holds the current value each time round, starting with the first and finishing with the last.

Understanding Looping through a list with for...in in VCE Software Development U3O1

What it does: The variable name holds a different value from names on each pass, so print(name) shows every value in order. The if is checked for every age in the list, but count only goes up when the age is 13 or more. A for loop line must end with a colon (:). Without it Python gives a syntax error. A for...in loop runs the indented block once for every value in the list. This list has 3 values.

Common mistakes: The loop variable is called pet, so item does not exist and causes a NameError. Inside the loop, use the loop variable: print(pet). nums is the whole list, so you cannot add it to a number. Add the loop variable n, which holds one value each time: total = total + n.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 72–96) and word search (Modules 72–113).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which line correctly loops through a list called pets?
for pet in pets:for pet in pets
Matches
for fruit in fruits print(fruit)
↓
The first line is missing the colon at the end. It should be for fruit in fruits:
Applying
Meanings
Term
for item in my_list:
Meaning
Runs the indented block once for each value in the list. The variable item holds the current value each time round, starting with the first and finishing with the last.
Practice
scores = [45, 72, 38, 90]
Testing
Gaps
item in mylist:
Check Answer
Spots
2for pet in pets:
3 print(item)

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Moose' badge — Tundra Champion, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which line correctly loops through a list called pets?
A. for pet in pets
B. for pet in pets: ✓
A for loop line must end with a colon (:). Without it Python gives a syntax error.
2. scores = [5, 8, 9] for s in scores: print(s) How many times does print(s) run?
A. 3 times, once for each value in the list ✓
B. 1 time, only for the first value
A for...in loop runs the indented block once for every value in the list. This list has 3 values.
3. In the loop for fruit in fruits:, what does the variable fruit hold?
A. The current value from the list on each time round ✓
B. The whole list all the time
The item variable holds one value at a time. It starts with the first value and finishes with the last.
4. Which loop prints every name in the list names?
A. for n in names: print(n) ✓
B. for n in names: print(n)
The code inside the loop must be indented. Without the indent, Python gives an indentation error.
5. colours = ["red", "blue"] for c in colours: print(c) What is printed first?
A. blue
B. red ✓
The loop starts with the first value in the list, which is "red".

Match the Pairs

for pet in pets: → Runs the indented block once for each value in the list pets, with pet holding the current value each time
for score in scores: if score > 50: → Loops through every score in the list and uses an if to check each one against 50
The item variable holds the current value from the list each time round the loop → for name in names: print(name)
Looping with an if inside only counts the items that pass the test → for age in ages: if age >= 13: count = count + 1
for fruit in fruits print(fruit) → The first line is missing the colon at the end. It should be for fruit in fruits:
for mark in marks: if mark > 50: print(mark) → The if line is not indented, so it is not inside the loop. It needs 4 spaces before it.

Key Term

for item in my_list:
Runs the indented block once for each value in the list. The variable item holds the current value each time round, starting with the first and finishing with the last.

Fill in the Blanks

1. for item in my_list: print(item)
Hint: Type the keyword that starts a loop over a list.
2. for item in my_list: print(item)
Hint: Type the keyword that joins the loop variable to the list.
3. for fruit in fruits: print(fruit)
Hint: Type the loop variable that holds the current value each time round.
4. for mark in marks: if mark >= 50: print(mark)
Hint: Type the comparison operator that checks if the mark is at least 50.
5. for n in numbers: if n > 10: print(n)
Hint: Type the keyword that makes the loop only print some values.

Spot the Error

Question 1
A pets = ["cat", "dog", "fish"]
B for pet in pets:
C print(item)
Line C — The loop variable is called pet, so item does not exist and causes a NameError. Inside the loop, use the loop variable: print(pet).
Question 2
A nums = [4, 9, 12, 7]
B total = 0
C for n in nums:
D total = total + nums
E print(total)
Line D — nums is the whole list, so you cannot add it to a number. Add the loop variable n, which holds one value each time: total = total + n.
Question 3
A fruits = ["apple", "pear"]
B for fruit in fruits
C print(fruit)
Line B — A for line must end with a colon. It should be 'for fruit in fruits:'.
Question 4
A ages = [10, 15, 13, 18]
B for age in ages:
C if age >= 13:
D print(ages, "teen")
Line D — ages is the whole list. Inside the loop, use the item variable age, which holds the current value. It should be print(age, "teen").
Question 5
A marks = [45, 80, 62]
B for mark in marks:
C if mark = 50:
D print("Pass")
Line C — A single = assigns a value. An if needs a comparison operator. It should be 'if mark >= 50:'.

Student with a class code? Join your class →