0

I have stumbled across the following issue(?) whilst debugging my code:

foo = {i: lambda: print(i) for i in range(10)}
foo[0]()
Output: 9

I was expecting the output to be 0 as this is what I passed as input to the lambda? I assume this is expected Python behaviour, but why? What is a pythonic way of achieving what I am trying to do? Cheers

Luke Taylor
  • 9,223
  • 13
  • 40
  • 72

0 Answers0