Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Implement a function with a default argument

Practice using default arguments safely by creating a function that appends an item to a list, avoiding the common mutable-default pitfall.

Python practice10 minFunctions & ScopeBeginnerLast updated March 17, 2026

Problem statement

Implement a function append_item(item, lst=None) that appends item to the list lst and returns the resulting list. If lst is omitted or None, the function should create and return a new list containing only item. Do not use a mutable object as a default argument (e.g., avoid lst=[] in the signature). The function must ensure separate calls with the default create distinct lists.

Task

Write append_item(item, lst=None) that appends item to lst. If lst is not provided, create a fresh list each call.

Examples

Default list creation

Input

append_item(1)

Output

[1]

No list provided; a new list [1] is created and returned.

Input format

First argument: item to append. Second optional argument: list to append to (or None to use a new list).

Output format

The list after appending the item.

Constraints

Ensure that a new list is created when lst is None. Do not mutate a shared default list across calls.

Samples

Sample 1

Input

append_item('a', ['x'])

Output

['x', 'a']

Appends 'a' to the provided list ['x'] and returns it.