← VCE Software Development U4O1

List comprehensions

A list comprehension builds a new list from a loop in one expression. Add 'if condition' to filter items, or change x (e.g. x * 2) to transform each item.

Understanding List comprehensions in VCE Software Development U4O1

What it does: This is the basic form [x for x in items]: loop over items and collect each x into a new list. The expression at the start (n.upper()) changes each item before it is added to the new list. A filter condition goes at the END of the comprehension, after the loop part: [m for m in marks if m >= 50]. A comprehension creates a new list. The original is only changed if you assign the result back to the same variable name. The 'if x > 10' part filters items. Only items that pass the condition are included in the new list.

Common mistakes: The keyword 'in' is missing. A list comprehension needs 'for x in xs' to loop over the list, so the line should be hi = [x for x in xs if x > 5]. Round brackets create a generator, not a list. A list comprehension uses square brackets: dbl = [p * 2 for p in prices].

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 82–114) and word search (Modules 3–114).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Which list comprehension doubles every number in nums?
[n for n in nums * 2][n * 2 for n in nums]
Matches
evens = [x for x in nums where x % 2 == 0]
↓
'where' is not Python. A condition in a list comprehension uses the keyword 'if'.
Applying
Meanings
Term
[x for x in items]
Meaning
A list comprehension builds a new list from a loop in one expression. Add 'if condition' to filter items, or change x (e.g. x * 2) to transform each item.
Practice
prices = [10, 25, 40]
Testing
Gaps
doubled = [x * 2 x in nums]
Check Answer
Spots
2# keep numbers bigger than 5
3hi = [x for x xs if x > 5]

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

The 'Kraken' badge — Deep Sea Master, 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 list comprehension doubles every number in nums?
A. [n for n in nums * 2]
B. [n * 2 for n in nums] ✓
The expression before 'for' says what each new item is, so n * 2 goes at the start. Putting * 2 after 'in' would repeat the whole list instead.
2. Which comprehension keeps only the marks that are 50 or more?
A. [m for m in marks if m >= 50] ✓
B. [m if m >= 50 for m in marks]
A filter condition goes at the END of the comprehension, after the loop part: [m for m in marks if m >= 50].
3. True or false: a list comprehension builds a NEW list and leaves the original list unchanged.
A. False
B. True ✓
A comprehension creates a new list. The original is only changed if you assign the result back to the same variable name.
4. Which line is the correct list comprehension syntax?
A. squares = [x * x for x in items] ✓
B. squares = [for x in items: x * x]
A list comprehension has the form [expression for variable in items]. The colon style belongs to a normal for loop, not a comprehension.
5. Given names = ['ava', 'liam'], which comprehension makes ['AVA', 'LIAM']?
A. [n.upper() for n in names] ✓
B. [n for n in names.upper()]
This transforms each item, so call .upper() on each n at the start. Calling .upper() on the list itself causes an AttributeError, because lists have no upper() method.

Match the Pairs

[x * 2 for x in nums] → Builds a new list with every number in nums doubled
[x for x in nums if x > 10] → Builds a new list with only the numbers in nums greater than 10
A list comprehension copies every item from a loop into a new list in one expression → copy = [x for x in items]
A list comprehension can transform each item, for example by making each name uppercase → upper = [n.upper() for n in names]
evens = [x for x in nums where x % 2 == 0] → 'where' is not Python. A condition in a list comprehension uses the keyword 'if'.
doubled = (x * 2 for x in nums) → Round brackets make a generator, not a list. Square brackets are needed to build a list.

Key Term

[x for x in items]
A list comprehension builds a new list from a loop in one expression. Add 'if condition' to filter items, or change x (e.g. x * 2) to transform each item.

Fill in the Blanks

1. doubled = [x * 2 for x in nums]
Hint: Type the keyword that starts the loop part inside the square brackets.
2. evens = [n for n in nums if n % 2 == 0]
Hint: Type the keyword that adds a condition to filter which items are kept.
3. squares = [n * n for n in range(1, 6)]
Hint: Type the expression at the front that transforms each item into its square.
4. marks = [55, 82, 47, 90] passes = [m for m in marks if m >= 50]
Hint: Type the comparison operator so only marks of 50 or more are kept.
5. names = ["ava", "liam"] big = [n.upper() for n in names]
Hint: Type the string method name that changes each item to capital letters.

Spot the Error

Question 1
A xs = [3, 8, 1, 9, 6]
B # keep numbers bigger than 5
C hi = [x for x xs if x > 5]
D print(hi)
Line C — The keyword 'in' is missing. A list comprehension needs 'for x in xs' to loop over the list, so the line should be hi = [x for x in xs if x > 5].
Question 2
A prices = [5, 10, 20]
B # double every price
C dbl = (p * 2 for p in prices)
D print(dbl)
Line C — Round brackets create a generator, not a list. A list comprehension uses square brackets: dbl = [p * 2 for p in prices].
Question 3
A pets = ['cat', 'dog', 'emu']
B up = [p.upper for p in pets]
C print(up)
Line B — upper is a method, so it needs brackets to be called. Without them the list holds method objects instead of uppercase text. It should be p.upper().
Question 4
A nums = [1, 2, 3, 4]
B sq = [n for n in nums]
C print(sq)
Line B — The expression at the front says how each item is transformed. Using just n copies the list unchanged. To square each item it should be n * n.
Question 5
A words = ['hi', 'yes', 'hello']
B print('Lengths:')
C # one length per word
D lens = [len(w) for w in word]
E print(lens)
Line D — The list is called words, not word, so Python raises a NameError. The loop part must name the real list: for w in words.

Student with a class code? Join your class →