What is printed after both calls?
def add(x, lst=[]):
lst.append(x)
return lst
print(add(1))
print(add(2))
- [1] [2]
- [1, 2] [1, 2]
- [1] [1]
- [1] [1, 2]
The default list is shared across calls, so it accumulates.
Play Problem Solving ▸
def add(x, lst=[]):
lst.append(x)
return lst
print(add(1))
print(add(2))