Track a list used as a default argument

A route-planning prototype uses this Python function. The method append adds one entry to a list, and len gives the number of entries in that list.


Python
def add_stop(stop, route=[]):
    route.append(stop)
    print(len(route))

# Call 1
add_stop("Depot")

# Call 2
add_stop("Library", [])

# Call 3
add_stop("Park")

# Call 4
add_stop("Pool", ["Dock"])

# Call 5
add_stop("Hall")


The function definition executes once, followed by these five calls in order, with no other calls or changes. For each call, enter the printed count and the number of entries in the list stored as the default value for route after that call. Track that default list even when a call explicitly supplies another list. Enter exact integers.

CallPrinted countDefault list length after call
1
2
3
4
5

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Parameters and Arguments Questions: