Editor

Fix the Mutable Default

Intermediate · Fix the bug · from Python *args, **kwargs, Default Values, and Keyword Arguments

From the lesson: Python *args, **kwargs, Default Values, and Keyword Arguments

Think about ordering coffee. You always need to pick a size - that's required. But extras like oat milk, an extra shot, or caramel drizzle? Those are optional. You only mention them when you want them.

Read the lesson

This function is supposed to create a new list each time, but it has the mutable default bug. Fix it so each call starts with a fresh list.

Expected output:

[1]
[2]
[3]
def append_to(value, items=[]):
items.append(value)
return items
print(append_to(1))
print(append_to(2))
print(append_to(3))

What Check runs

Your code runs first, then each of these lines. The output must match exactly.

RunsExpected output
print(append_to(1))[1]
print(append_to(2))[2]
print(append_to(3))[3]

More Function Arguments exercises

All Python exercises · 50 practice problems · Functions quiz