def append_to_lst(val, lst=[]):
lst.append(val)
return lst
print(append_to_lst(1))
print(append_to_lst(2))
print(append_to_lst(3, []))
Which of the following is the correct output of this program?This is the classic Python "mutable default argument" gotcha, and the cleanest way to solve it is to track exactly one thing: which list object each call is actually appending to, the shared default, or a fresh one passed in explicitly.
Python evaluates $lst=[]$ exactly once, when the function is defined, not once per call. That single list object then lives on as long as the function exists, and any call that skips the second argument automatically falls back to reusing that same object, carrying forward whatever was appended to it in earlier calls.
Stacking the three printed results in order: $[1]$, then $[1, 2]$, then $[3]$. That sequence corresponds to option (B).
A quick way to remember this for future code review: never use a mutable object like a list or dict as a default argument value unless you actually want it shared and remembered across calls, use $None$ as the default and create a fresh list inside the function body instead.
\[ \boxed{[1],\ [1,2],\ [3]} \]