List comprehension and lambdas in Python [duplicate]

I wanted to create a list of lambdas, but it didn't quite work out as I hoped.

L = [(lambda x: x/y) for y in range(10)]

I expected every function in the list to divide its argument by its index, but all functions only divide by the last index.

>>> L[1](5)
0.5555555555555556
>>> L[5](5)
0.5555555555555556
>>> 5/9
0.5555555555555556

Is this kind of list comprehension, where every lambda has its own copy of ypossible in Python?


The y in your lambda refers to the last value that y had in the scope it came from, i.e., 9.

The easiest way to get the behavior you want is to use a default argument in your lambda:

lambda x, y=y: x/y

This captures the value of y at the moment the lambda function is defined.

You can also do a "double-lambda", calling a function that returns the lambda you want, passing in the desired value of y:

(lambda y: lambda x: x/y)(y)

Here, the outer lambda provides a new scope each time you call it.


You need the loop and the lambda to be in different scopes.

def make_divider(y):
    return lambda x: x / y

L = [make_divider(y) for y in range(10)]

print(L[2](5) == 5 / 2)
print(L[4](5) == 5 / 4)