1

I am trying to generate a list of functions in python3

l=[lambda r: r+i for i in range(3)]
l[0](1) #=3
l[1](1) #=3
l[2](1) #=3

However, when I am using the code above I always get the same result. It seems like the function is always the one i created last. In this case lambda r: r+2

Any idea how I could generate a list of functions is appreciated.

jan-seins
  • 1,253
  • 1
  • 18
  • 31
  • 1
    For this case, you could solve this (in a somewhat contrived way) for example with `l = [(lambda i: lambda r: r+i)(i) for i in range(3)]` or just `l = [lambda r, i=i: r+i for i in range(3)]`. – jdehesa Apr 26 '19 at 16:27

0 Answers0