Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a list of lambdas (in a list comprehension/for loop)?

I want to create a list of lambda objects from a list of constants in Python; for instance:

listOfNumbers = [1,2,3,4,5] square = lambda x: x * x listOfLambdas = [lambda: square(i) for i in listOfNumbers] 

This will create a list of lambda objects, however, when I run them:

for f in listOfLambdas:     print f(), 

I would expect that it would print

1 4 9 16 25 

Instead, it prints:

25 25 25 25 25 

It seems as though the lambdas have all been given the wrong parameter. Have I done something wrong, and is there a way to fix it? I'm in Python 2.4 I think.

EDIT: a bit more of trying things and such came up with this:

listOfLambdas = [] for num in listOfNumbers:     action = lambda: square(num)     listOfLambdas.append(action)     print action() 

Prints the expected squares from 1 to 25, but then using the earlier print statement:

for f in listOfLambdas:     print f(), 

still gives me all 25s. How did the existing lambda objects change between those two print calls?

Related question: Why results of map() and list comprehension are different?

like image 698
Smashery Avatar asked Jan 17 '09 01:01

Smashery


People also ask

Can you use lambda in list comprehension?

List comprehension is used to create a list. Lambda function process is the same as other functions and returns the value of the list. List comprehension is more human-readable than the lambda function.

What are lambda expressions What are list comprehensions?

The difference between Lambda and List Comprehension. List Comprehension is used to create lists, Lambda is function that can process like other functions and thus return values or lists.

How do you do a for loop in a lambda function?

Since a for loop is a statement (as is print , in Python 2. x), you cannot include it in a lambda expression. Instead, you need to use the write method on sys. stdout along with the join method.


2 Answers

You have:

listOfLambdas = [lambda: i*i for i in range(6)]  for f in listOfLambdas:     print f() 

Output:

25 25 25 25 25 25 

You need currying! Aside from being delicious, use this default value "hack".

listOfLambdas = [lambda i=i: i*i for i in range(6)]  for f in listOfLambdas:     print f() 

Output:

0 1 4 9 16 25 

Note the i=i. That's where the magic happens.

like image 121
recursive Avatar answered Sep 17 '22 13:09

recursive


I'm guessing that the lambda you're creating in the list comprehension is bound to the variable i which eventually ends up at 5. Thus, when you evaluate the lambdas after the fact, they're all bound to 5 and end up calculating 25. The same thing is happening with num in your second example. When you evaluate the lambda inside the loop it's num hasn't changed so you get the right value. After the loop, num is 5...

I'm not quite sure what you're going for, so I'm not sure how to suggest a solution. How about this?

def square(x): return lambda : x*x listOfLambdas = [square(i) for i in [1,2,3,4,5]] for f in listOfLambdas: print f() 

This gives me the expected output:

1 4 9 16 25 

Another way to think of this is that a lambda "captures" its lexical environment at the point where it is created. So, if you give it num it doesn't actually resolve that value until its invoked. This is both confusing and powerful.

like image 43
Dave Ray Avatar answered Sep 18 '22 13:09

Dave Ray