3

I'm playing with lambda functions inside of list comprehension, and found some weird behaviour

x = [(lambda x: i) for i in range(3)]

print(x[0](0)) #print 2 instead of 0
print(x[1](0)) #print 2 instead of 1
print(x[2](0)) #print 2

Can someone explain why the result is not that I expect?

Nelson Tatius
  • 7,251
  • 7
  • 43
  • 66

1 Answers1

9

lambdas bind variables themselves, not the values that they had. i is changed to 2 at the end of the list comprehension, so all the lambdas refer to i at that point, and thus refer to 2.

To avoid this, you can use the default argument trick:

[lambda x,i=i:i for i in range(3)]

This binds the value of i in the default argument (which is evaluated at function definition time).

nneonneo
  • 162,933
  • 34
  • 285
  • 360
  • +1 for clarity and brevity – gonz Apr 04 '13 at 15:29
  • `[lambda x,j=i:j for i in range(3)]` would be even clearer. – Johan Råde Apr 04 '13 at 17:21
  • if you want to use lambda to define a function, then apply a variable to it. The variable in the lambda is not what the OP thinks it is, despite having the same symbol. So this is probably what the OP wants: `[ (lambda a:a)(i) for i in range(3)]` – Tim Richardson Nov 12 '17 at 12:18