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 4015290
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:36:04+00:00 2026-05-20T09:36:04+00:00

I am new in Python. My task was quite simple — I need a

  • 0

I am new in Python. My task was quite simple — I need a list of functions that I can use to do things in batch. So I toyed it with some examples like

fs = [lambda x: x + i for i in xrange(10)]

Surprisingly, the call of

[f(0) for f in fs]

gave me the result like [9, 9, 9, 9, 9, 9, 9, 9, 9, 9]. It was not what I expected as I’d like the variable i has different values in different functions.

So My question is:

  1. Is the variable i in lambda global or local?

  2. Does python has the same concept like ‘closure’ in javascript? I mean does each lambda here holds a reference to the i variable or they just hold a copy of the value of i in each?

  3. What should I do if I’d like the output to be [0, 1, .....9] in this case?

  • 1 1 Answer
  • 0 Views
  • 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. Editorial Team
    Editorial Team
    2026-05-20T09:36:05+00:00Added an answer on May 20, 2026 at 9:36 am

    It looks a bit messy, but you can get what you want by doing something like this:

    >>> fs = [(lambda y: lambda x: x + y)(i) for i in xrange(10)]
    >>> [f(0) for f in fs]
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    

    Normally Python supports the “closure” concept similar to what you’re used to in Javascript. However, for this particular case of a lambda expression inside a list comprehension, it seems as though i is only bound once and takes on each value in succession, leaving each returned function to act as though i is 9. The above hack explicitly passes each value of i into a lambda that returns another lambda, using the captured value of y.

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

Sidebar

Related Questions

Am new in Python and would like to do a task... I need to
The latest Google App Engine release supports a new Task Queue API in Python.
I'm new to python, and have a list of longs which I want to
I am new to python. I have a task to access wikipedia. For this
I'm new to javascript and have a simple task. I have an array values
i am new to python and learning it for my job functions. im following
I'm fairly new to C, not having much need to anything faster than python
I use aa python program which inserts many new entries to database, this new
I am new to Python, and my current task is to write a web
I have a custom c++ module for python that exposes functions, some of which

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.