List comprehensions

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

List comprehensions

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which list comprehension doubles every number in nums?
A [n for n in nums * 2]
B [n * 2 for n in nums]
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]
3. True or false: a list comprehension builds a NEW list and leaves the original list unchanged.
A False
B True
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]
5. Given names = ['ava', 'liam'], which comprehension makes ['AVA', 'LIAM']?
A [n.upper() for n in names]
B [n for n in names.upper()]

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1[x * 2 for x in nums] ABuilds a new list with every number in nums doubled
2[x for x in nums if x > 10] BBuilds a new list with only the numbers in nums greater than 10
3A list comprehension copies every item from a loop into a new list in one expression CRound brackets make a generator, not a list. Square brackets are needed to build a list.
4A list comprehension can transform each item, for example by making each name uppercase Dupper = [n.upper() for n in names]
5evens = [x for x in nums where x % 2 == 0] Ecopy = [x for x in items]
6doubled = (x * 2 for x in nums) F'where' is not Python. A condition in a list comprehension uses the keyword 'if'.

Definitions

[x for x in items]

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
doubled = [x * 2 ___ x in nums]
2.
evens = [n for n in nums __ n % 2 == 0]
3.
squares = [_____ for n in range(1, 6)]
4.
marks = [55, 82, 47, 90] passes = [m for m in marks if m __ 50]
5.
names = ["ava", "liam"] big = [n._____() for n in names]

Spot the Error

Shade in the letter of the line that contains an 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)
Question 2
A prices = [5, 10, 20]
B # double every price
C dbl = (p * 2 for p in prices)
D print(dbl)
Question 3
A pets = ['cat', 'dog', 'emu']
B up = [p.upper for p in pets]
C print(up)
Question 4
A nums = [1, 2, 3, 4]
B sq = [n for n in nums]
C print(sq)
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)

Answer Key

List comprehensions
Section A: Quick Questions
Q1
B — [n * 2 for n in nums]
Q2
A — [m for m in marks if m >= 50]
Q3
B — True
Q4
A — squares = [x * x for x in items]
Q5
A — [n.upper() for n in names]
Section B: Match the Pairs
Q1
[x * 2 for x in nums] → Builds a new list with every number in nums doubled
Q2
[x for x in nums if x > 10] → Builds a new list with only the numbers in nums greater than 10
Q3
A list comprehension copies every item from a loop into a new list in one expression → copy = [x for x in items]
Q4
A list comprehension can transform each item, for example by making each name uppercase → upper = [n.upper() for n in names]
Q5
evens = [x for x in nums where x % 2 == 0] → 'where' is not Python. A condition in a list comprehension uses the keyword 'if'.
Q6
doubled = (x * 2 for x in nums) → Round brackets make a generator, not a list. Square brackets are needed to build a list.
Section C: Definitions
Q1
[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.
Section D: Fill in the Blanks
Q1
doubled = [x * 2 for x in nums]
Q2
evens = [n for n in nums if n % 2 == 0]
Q3
squares = [n * n for n in range(1, 6)]
Q4
marks = [55, 82, 47, 90] passes = [m for m in marks if m >= 50]
Q5
names = ["ava", "liam"] big = [n.upper() for n in names]
Section E: Spot the Error
Q1
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].
Q2
C — Round brackets create a generator, not a list. A list comprehension uses square brackets: dbl = [p * 2 for p in prices].
Q3
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().
Q4
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.
Q5
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.