practicepython.in
python exercises
171 exercises

Daily #4 · 2 October 2026

Three functions, one number Hard

What does this print?

funcs = [lambda: i for i in range(3)]
print([f() for f in funcs])
  1. A[2, 2, 2]
  2. B[0, 1, 2]
  3. Craises NameError
  4. D[3, 3, 3]
Show the answer

A: [2, 2, 2]

Why: Each lambda looks up i when it is called, not when it is made. By then the loop has finished and i is 2 for all three. lambda i=i: i freezes the value at creation.

Play today’s puzzle →