Functions that change a list or return a new one

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

Functions that change a list or return a new one

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A function runs scores.append(10) on a list that was passed in as scores. What happens to the original list?
A The original list now has 10 added to it
B The original list stays the same
2. Which line makes a separate copy of the list items?
A new_items = items
B new_items = items[:]
3. A function does items.append(5) and has no return statement. Which description fits it?
A It changes the list in place and returns None
B It returns a new list and leaves the original alone
4. You want add_bonus(marks) to keep the original marks list safe. Which is the best first line inside the function?
A result = marks
B result = marks[:]
5. After this code runs, what is nums? def add_one(items): items.append(1) nums = [4, 5] add_one(nums)
A [4, 5, 1]
B [4, 5]

Match the Pairs

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

Left columnRight column
1def add_item(items): items.append(5) Acopy = items does not make a copy. Both names point to the same list, so the original is still changed. Use items[:] instead.
2copy = items[:] Bdef clear_first(items): items.pop(0) nums = [1, 2, 3] clear_first(nums)
3To keep the original list safe, copy it inside the function and return the changed copy CMakes a new separate list with the same values, so changes to the copy do not affect the original
4Passing a list to a function and changing it inside means the caller's list is also changed DChanges the original list in place, because append works on the same list that was passed in
5def add_item(items): copy = items copy.append(5) return copy Edef add_bonus(scores): new_scores = scores[:] new_scores.append(10) return new_scores
6def add_item(items): items.append(5) nums = add_item([1, 2]) FThe function changes the list in place and returns nothing, so nums is None. Either return the list or use the original list after the call.

Definitions

copy = items[:]

Fill in the Blanks

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

1.
def bonus(marks): marks.______(10) print(marks)
2.
def safe(items): copy = items[_] copy.append(1)
3.
def extend(items): new = items[:] + [9] ______ new
4.
scores = [7, 8] grow(scores) print(______)
5.
backup = ______[:] grow(scores) print(backup)

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A def add_bonus(scores)
B scores.append(100)
C return scores
D marks = [70, 80]
Question 2
A def add_item(items, x):
B items.add(x)
C nums = [1, 2]
D add_item(nums, 3)
E print(nums)
Question 3
A def with_extra(items, x):
B new_items = items
C new_items.append(x)
D return new_items
Question 4
A def add_zero(items):
B copy = items[:]
C items.append(0)
D return copy
Question 5
A def remove_last(items):
B result = items[:]
C result.pop
D return result

Answer Key

Functions that change a list or return a new one
Section A: Quick Questions
Q1
A — The original list now has 10 added to it
Q2
B — new_items = items[:]
Q3
A — It changes the list in place and returns None
Q4
B — result = marks[:]
Q5
A — [4, 5, 1]
Section B: Match the Pairs
Q1
def add_item(items): items.append(5) → Changes the original list in place, because append works on the same list that was passed in
Q2
copy = items[:] → Makes a new separate list with the same values, so changes to the copy do not affect the original
Q3
To keep the original list safe, copy it inside the function and return the changed copy → def add_bonus(scores): new_scores = scores[:] new_scores.append(10) return new_scores
Q4
Passing a list to a function and changing it inside means the caller's list is also changed → def clear_first(items): items.pop(0) nums = [1, 2, 3] clear_first(nums)
Q5
def add_item(items): copy = items copy.append(5) return copy → copy = items does not make a copy. Both names point to the same list, so the original is still changed. Use items[:] instead.
Q6
def add_item(items): items.append(5) nums = add_item([1, 2]) → The function changes the list in place and returns nothing, so nums is None. Either return the list or use the original list after the call.
Section C: Definitions
Q1
copy = items[:] — A function can change a list you pass in, altering the original in place. Make a copy with [:] first if you need to keep the original list unchanged.
Section D: Fill in the Blanks
Q1
def bonus(marks): marks.append(10) print(marks)
Q2
def safe(items): copy = items[:] copy.append(1)
Q3
def extend(items): new = items[:] + [9] return new
Q4
scores = [7, 8] grow(scores) print(scores)
Q5
backup = scores[:] grow(scores) print(backup)
Section E: Spot the Error
Q1
A — A function definition must end with a colon. It should be def add_bonus(scores):
Q2
B — Lists have no add() method. Use append() to add an item to the end of the list, so the original list is changed in place.
Q3
B — This does not copy the list. Both names point to the same list, so append would change the original. Use items[:] to make a real copy.
Q4
C — This appends to the original list instead of the copy. The function should call copy.append(0) so the original stays unchanged.
Q5
C — Without brackets, pop is not called, so nothing is removed. It should be result.pop().