← ACARA Digital Technologies Years 9–10

Functions that change a list or return a new one

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.

Understanding Functions that change a list or return a new one in ACARA Digital Technologies Years 9–10

What it does: The function changes only new_scores, a copy, and returns it, so the original scores list stays the same. items and nums refer to the same list, so after the call nums becomes [2, 3]. append() changes the list object itself, so the caller's list is altered even though nothing is returned. Slicing with [:] copies the whole list, giving a new list you can change safely. Assignment only gives the list a second name, whereas items[:] builds a new list. Functions without a return statement give back None, so nums does not hold the list.

Common mistakes: A function definition must end with a colon. It should be def add_bonus(scores):. 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.

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
A function runs scores.append(10) on a list that was passed in as scores. What happens to the original list?
The original list now has 10 added to itThe original list stays the same
Matches
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.
Applying
Meanings
Term
copy = items[:]
Meaning
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.
Practice
def add_bonus(marks):
Testing
Gaps
marks.(10)
Check Answer
Spots
1def add_bonus(scores)
2 scores.append(100)

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

The 'Viperfish' badge — Deep Debugger, 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. 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
A list is passed by reference, so append() inside the function changes the original list in place.
2. Which line makes a separate copy of the list items?
A. new_items = items
B. new_items = items[:] ✓
items[:] builds a new list with the same values. new_items = items only gives the same list a second name, so both names point to the one list.
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
Without a return, a function gives back None. Any change it made to the list happens in place on the original.
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[:] ✓
Copy the list first, then change the copy. Plain assignment would still point at the original list.
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]
add_one changes the list it was given, so nums is altered even though the function returns nothing.

Match the Pairs

def add_item(items): items.append(5) → Changes the original list in place, because append works on the same list that was passed in
copy = items[:] → Makes a new separate list with the same values, so changes to the copy do not affect the original
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
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)
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.
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.

Key Term

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.

Fill in the Blanks

1. def bonus(marks): marks.append(10) print(marks)
Hint: Type the list method that adds an item to the end of the list. It changes the original list in place.
2. def safe(items): copy = items[:] copy.append(1)
Hint: Type the single symbol between the square brackets that slices the whole list into a copy, so the original stays safe.
3. def extend(items): new = items[:] + [9] return new
Hint: Type the keyword that sends the new list back to the caller instead of changing the original.
4. scores = [7, 8] grow(scores) print(scores)
Hint: Type the name of the list variable that was passed into the function. Printing it shows the original list was altered in place.
5. backup = scores[:] grow(scores) print(backup)
Hint: Type the name of the list variable being copied before the function changes it.

Spot the Error

Question 1
A def add_bonus(scores)
B scores.append(100)
C return scores
D marks = [70, 80]
Line A — A function definition must end with a colon. It should be def add_bonus(scores):
Question 2
A def add_item(items, x):
B items.add(x)
C nums = [1, 2]
D add_item(nums, 3)
E print(nums)
Line 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.
Question 3
A def with_extra(items, x):
B new_items = items
C new_items.append(x)
D return new_items
Line 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.
Question 4
A def add_zero(items):
B copy = items[:]
C items.append(0)
D return copy
Line C — This appends to the original list instead of the copy. The function should call copy.append(0) so the original stays unchanged.
Question 5
A def remove_last(items):
B result = items[:]
C result.pop
D return result
Line C — Without brackets, pop is not called, so nothing is removed. It should be result.pop().

Student with a class code? Join your class →