← ACARA Digital Technologies Years 9–10

Building and returning a list from a function

Start a function with an empty list, append each wanted item inside a loop, then return the finished list after the loop ends. Filtering builds a new list.

Understanding Building and returning a list from a function in ACARA Digital Technologies Years 9–10

What it does: Only marks that pass the if test are appended, so the new list holds a filtered selection. The return is outside the loop, so it runs once after every item has been added. An empty list [] gives you somewhere to collect items. A number like 0 cannot use append(). append() adds the item to the end of the list. Using = replaces the whole list with a single value. The function only reads the original list and appends wanted items to result, so the original is untouched. The accumulator list starts empty so each wanted item can be appended to it later.

Common mistakes: An if statement must end with a colon. The line should be: if m >= 50:. {} makes an empty dictionary, not a list, so append() would fail. Start with an empty list: evens = [].

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 629–680) and word search (Modules 629–707).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Which line correctly starts a function that will build a list of results?
result = 0result = []
Matches
def get_evens(nums): result = [] for n in nums: if n % 2 == 0: result.append(n) return result
↓
The return is indented inside the loop, so the function exits after the first item and returns a list with at most one item.
Applying
Meanings
Term
result = []
Meaning
Start a function with an empty list, append each wanted item inside a loop, then return the finished list after the loop ends. Filtering builds a new list.
Practice
def get_passes(marks):
Testing
Gaps
result []
Check Answer
Spots
3 for m in marks:
4 if m >= 50

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

The 'Badger' badge — Determined Digger, 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 starts a function that will build a list of results?
A. result = 0
B. result = [] ✓
An empty list [] gives you somewhere to collect items. A number like 0 cannot use append().
2. Where should the return statement go when a function builds a list in a loop?
A. Inside the loop, under append()
B. After the loop, lined up with the for ✓
Return after the loop so every item is checked first. Return inside the loop ends the function on the first pass.
3. Which code adds a wanted item to the list called result?
A. result.append(n) ✓
B. result = n
append() adds the item to the end of the list. Using = replaces the whole list with a single value.
4. Which function returns a new list of only the even numbers? A: def evens(nums): result = [] for n in nums: if n % 2 == 0: result.append(n) return result B: def evens(nums): for n in nums: result = [] if n % 2 == 0: result.append(n) return result
A. A ✓
B. B
In B the empty list is created inside the loop, so it resets on every pass. Create it once before the loop.
5. What does this function return for big([3, 9, 5, 12])? def big(nums): result = [] for n in nums: if n > 6: result.append(n) return result
A. [9, 12] ✓
B. [3, 5]
Only numbers greater than 6 are appended, so the new list is [9, 12].

Match the Pairs

result = [] → Creates an empty list at the start of the function to collect the answers
result.append(mark) → Adds the current item to the end of the list being built inside the loop
Filtering: only keep items that pass a test by appending them inside an if statement → for mark in marks: if mark >= 50: passes.append(mark)
Return the finished list after the loop ends, at the same indent level as the for loop → for n in nums: result.append(n * 2) return result
def get_evens(nums): result = [] for n in nums: if n % 2 == 0: result.append(n) return result → The return is indented inside the loop, so the function exits after the first item and returns a list with at most one item.
def get_big(nums): for n in nums: result = [] if n > 10: result.append(n) return result → The empty list is created inside the loop, so it resets every time and the earlier items are lost.

Key Term

result = []
Start a function with an empty list, append each wanted item inside a loop, then return the finished list after the loop ends. Filtering builds a new list.

Fill in the Blanks

1. def keep(marks): result = [] for m in marks:
Hint: Type the operator that stores the empty list in the variable before the loop starts.
2. for n in numbers: if n > 50: result.append(n)
Hint: Type the list method that adds one item to the end of the list.
3. for n in numbers: result.append(n) return result
Hint: Type the keyword that sends the finished list back, placed after the loop.
4. for w in words: result.append(w) return result
Hint: Type the name of the variable that holds the finished list.
5. for mark in marks: if mark >= 50: result.append(mark)
Hint: Type the comparison operator meaning greater than or equal to, so only passing marks are kept.

Spot the Error

Question 1
A def get_passes(marks):
B passed = []
C for m in marks:
D if m >= 50
E passed.append(m)
F return passed
Line D — An if statement must end with a colon. The line should be: if m >= 50:
Question 2
A def get_evens(nums):
B evens = {}
C for n in nums:
D if n % 2 == 0:
E evens.append(n)
F return evens
Line B — {} makes an empty dictionary, not a list, so append() would fail. Start with an empty list: evens = []
Question 3
A def get_long_words(words)
B long_words = []
C for w in words:
D if len(w) > 5:
E long_words.append(w)
F return long_words
Line A — A function definition must end with a colon. It should be: def get_long_words(words):
Question 4
A def get_adults(ages):
B adults = []
C for age of ages:
D if age >= 18:
E adults.append(age)
F return adults
Line C — Python for loops use the keyword in, not of. It should be: for age in ages:
Question 5
A def square_all(nums):
B squares = []
C for n in nums:
D squares.add(n * n)
E return squares
Line D — Lists do not have an add() method. To put an item on the end of a list, use append(). It should be: squares.append(n * n)

Student with a class code? Join your class →