Building and returning a list from a function

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

Building and returning a list from a function

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line correctly starts a function that will build a list of results?
A result = 0
B result = []
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
3. Which code adds a wanted item to the list called result?
A result.append(n)
B result = n
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
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]

Match the Pairs

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

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

Definitions

result = []

Fill in the Blanks

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

1.
def keep(marks): result _ [] for m in marks:
2.
for n in numbers: if n > 50: result.______(n)
3.
for n in numbers: result.append(n) ______ result
4.
for w in words: result.append(w) return ______
5.
for mark in marks: if mark __ 50: result.append(mark)

Spot the Error

Shade in the letter of the line that contains an 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
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
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
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
Question 5
A def square_all(nums):
B squares = []
C for n in nums:
D squares.add(n * n)
E return squares

Answer Key

Building and returning a list from a function
Section A: Quick Questions
Q1
B — result = []
Q2
B — After the loop, lined up with the for
Q3
A — result.append(n)
Q4
A — A
Q5
A — [9, 12]
Section B: Match the Pairs
Q1
result = [] → Creates an empty list at the start of the function to collect the answers
Q2
result.append(mark) → Adds the current item to the end of the list being built inside the loop
Q3
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)
Q4
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
Q5
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.
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
def keep(marks): result = [] for m in marks:
Q2
for n in numbers: if n > 50: result.append(n)
Q3
for n in numbers: result.append(n) return result
Q4
for w in words: result.append(w) return result
Q5
for mark in marks: if mark >= 50: result.append(mark)
Section E: Spot the Error
Q1
D — An if statement must end with a colon. The line should be: if m >= 50:
Q2
B — {} makes an empty dictionary, not a list, so append() would fail. Start with an empty list: evens = []
Q3
A — A function definition must end with a colon. It should be: def get_long_words(words):
Q4
C — Python for loops use the keyword in, not of. It should be: for age in ages:
Q5
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)