Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 121721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T04:00:56+00:00 2026-05-11T04:00:56+00:00

I want to create a list of lambda objects from a list of constants

  • 0

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?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T04:00:56+00:00Added an answer on May 11, 2026 at 4:00 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

in my python code i want to create a list of lists...in a loop
I`v come across a need where I want to create multiple list items from
I want to create list view just like this: http://dl.maximumpc.com/galleries/androidpower/Alarm_full.jpg I need to create
I want to create dynamic lambda expressions so that I can filter a list
I want to create list of years in array like this: array ( '2011'
I want to create a list of integers from 1 to n . I
I am new in ubuntu 12.04 LTS . I want to create list file
I want to create a list within a list item on jQuery mobile here's
I want to create a list of category terms (contained within a block), with
I want to create a list of GridBagPanel.Constraints . I read it in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.