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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:10:18+00:00 2026-05-29T11:10:18+00:00

Read a question on stack overflow sometime back with the following syntax In [1]:

  • 0

Read a question on stack overflow sometime back with the following syntax

In [1]: [lambda: x for x in range(5)][0]()
Out[1]: 4
In [2]: [lambda: x for x in range(5)][2]()
Out[2]: 4

But i am having a hard time to understand why exactly the output of this comes as 4,
my understanding is it always gives the last value of the list as output,

In [4]: [lambda: x for x in [1,5,7,3]][0]()
Out[4]: 3

but still not convinced how does this syntax ends up with the last value.

Would be very glad if i can get a proper explanation for this syntax

  • 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-29T11:10:19+00:00Added an answer on May 29, 2026 at 11:10 am

    This isn’t really about either list comprehensions or lambdas. It’s about the scoping rules in Python. Let’s rewrite the list comprehension into an equivalent loop:

    funcs = []
    for x in range(5):
        def f(): return x
        funcs.append(f)
    funcs[0]() # returns 4
    

    Here, we can see that we successively construct functions and store them in a list. When one of these functions is called, all that happens is the value of x is looked up and returned. But x is a variable whose value changes, so the final value of 4 is what is always returned. You could even change the value of x after the loop, e.g.,

    x = 32 
    funcs[2]() # returns 32
    

    To get the behavior you expected, Python would need to scope the for contents as a block; it doesn’t. Usually, this isn’t a problem, and is easy enough to work around.

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

Sidebar

Related Questions

I read the system tray tutorial and this similar Stack Overflow question but can't
I have read other posts on stack overflow on the issue but my question
I read an answer to a question on Stack Overflow that contained the following
I read on another Stack Overflow question that Python was just like Ruby, as
Ok, I've read the Stack Overflow question 'What does it mean to program to
I've already asked similar question on this issue on stack overflow already, but I
I've read several of the posts on stack overflow already about this topic, but
I have spent a bit of time rummaging through Stack Overflow to find out
I read Stack Overflow question How do I convert a binary string to a
Thank you for taking the time to read this. I looked over Stack Overflow

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.