practicepython.in
python exercises
171 exercises

Daily #1 · 29 September 2026

The list that remembers Medium

What does this print?

def add(item, bucket=[]):
    bucket.append(item)
    return bucket

add("a")
print(add("b"))
  1. A['a', 'b']
  2. B['b']
  3. C['a']
  4. Draises TypeError
Show the answer

A: ['a', 'b']

Why: A default value is made once, when the function is defined, not each time it is called. Both calls append to the same list. The fix is bucket=None, then bucket = [] if bucket is None else bucket inside the function.

Play today’s puzzle →