$ ./guess_the_languageProblem Solving — predict the output
Problem Solving — predict the output · medium · #227

What is the output?

d = {}
d.setdefault('k', []).append(1)
d.setdefault('k', []).append(2)
print(d)
  1. {'k': [2]}
  2. {'k': [1, 2]}
  3. {'k': [1]}
  4. {'k': [[1], [2]]}
setdefault only inserts the default when the key is missing and always returns the stored list.
Play Problem Solving ▸