The first one creates a single lambda function and calls it ten times.

The second one doesn't call the function. It creates 10 different lambda functions. It puts all of those in a list. To make it equivalent to the first you need:

[(lambda x: x*x)(x) for x in range(10)]

Or better yet:

[x*x for x in range(10)]
Answer from Winston Ewert on Stack Overflow
🌐
W3Schools
w3schools.com › python › python_lambda.asp
Python Lambda
Lambda functions are commonly used with built-in functions like map(), filter(), and sorted().
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-lambda-anonymous-functions-filter-map-reduce
Python Lambda Functions - GeeksforGeeks
reduce() function repeatedly applies a lambda expression to elements of a list to combine them into a single result. ... The lambda multiplies two numbers at a time. ... In Python, both lambda and def can be used to define functions, but they ...
Published   1 week ago
Discussions

What is the purpose of Lambda expressions?
This is a typical example for beginners: x=lambda n:2*n print(x(7)) Otherwise I would create a function: def dbl(n): return 2*n print(dbl(7)) Of course: I can write simply 2*7, but the idea is to save a complex formula in an object once, and reuse it several times. More on discuss.python.org
🌐 discuss.python.org
0
1
December 8, 2021
python - Lambda function in list comprehensions - Stack Overflow
People gave good answers but forgot to mention the most important part in my opinion: In the second example the X of the list comprehension is NOT the same as the X of the lambda function, they are totally unrelated. More on stackoverflow.com
🌐 stackoverflow.com
Lambda function
map applies a function to every element of an iterable. Sometimes these functions are very simple operations; for example, if I wanted to double every number in a list. It's wasteful to define an entire new function (with the def keyword) just for this one simple operation. Lambda creates a callable function that we can use. Here's an example of how we could create that and use it with map. numbers = [1, 5, 20, 50] map(lambda x:x * 2, numbers) More on reddit.com
🌐 r/learnpython
24
11
September 9, 2023
How often do you guys use Lambda?
I kinda view them as a shorthand, not required but sometimes nice to have. Stuff like sorting list of lists by index makes sense to use lambdas, but you can fall into the trap of having unreadable oneliners, so some self-restraint is required. If you meant the AWS Lambda the service, I like it IF you have a good way of managing dependancies or are comfortable with using the standard lib for everything. More on reddit.com
🌐 r/Python
219
225
November 28, 2022
🌐
Amazon Web Services
docs.aws.amazon.com › aws lambda › developer guide › building lambda functions with python › define lambda function handler in python
Define Lambda function handler in Python - AWS Lambda
January 31, 2026 - This page describes how to work with Lambda function handlers in Python, including naming conventions, valid handler signatures, and code best practices. This page also includes an example of a Python Lambda function that takes in information about an order, produces a text file receipt, and puts this file in an Amazon Simple Storage Service (Amazon S3) bucket.
🌐
DataCamp
datacamp.com › tutorial › python-lambda-functions
Python Lambda Functions: A Beginner’s Guide | DataCamp
January 31, 2025 - Lambda functions are ideal for ... the same functionality can be achieved with a concise one-liner lambda function assigned to a variable: is_even = lambda x: x % 2 == 0....
🌐
Programiz
programiz.com › python-programming › anonymous-function
Python Lambda/ Function (With Examples)
In the above example, we have assigned a lambda function to the greet_user variable. Here, name after the lambda keyword specifies that the lambda function accepts the argument named name. ... Here, we have passed a string value 'Delilah' to our lambda function. Finally, the statement inside the lambda function is executed. ... The filter() function in Python takes in a function and an iterable (lists, tuples, and strings) as arguments.
🌐
Amazon Web Services
docs.aws.amazon.com › aws lambda › developer guide › building lambda functions with python
Building Lambda functions with Python - AWS Lambda
Runtime: Choose Python 3.14. Choose Create function. The console creates a Lambda function with a single source file named lambda_function. You can edit this file and add more files in the built-in code editor. In the DEPLOY section, choose Deploy to update your function's code.
🌐
Python.org
discuss.python.org › python help
What is the purpose of Lambda expressions? - Python Help - Discussions on Python.org
December 8, 2021 - This is a typical example for beginners: x=lambda n:2*n print(x(7)) Otherwise I would create a function: def dbl(n): return 2*n print(dbl(7)) Of course: I can write simply 2*7, but the idea is to save a complex formula in an object once, and ...
Find elsewhere
Top answer
1 of 7
370

The first one creates a single lambda function and calls it ten times.

The second one doesn't call the function. It creates 10 different lambda functions. It puts all of those in a list. To make it equivalent to the first you need:

[(lambda x: x*x)(x) for x in range(10)]

Or better yet:

[x*x for x in range(10)]
2 of 7
191

This question touches a very stinking part of the "famous" and "obvious" Python syntax - what takes precedence, the lambda, or the for of list comprehension.

I don't think the purpose of the OP was to generate a list of squares from 0 to 9. If that was the case, we could give even more solutions:

squares = []
for x in range(10): squares.append(x*x)
  • this is the good ol' way of imperative syntax.

But it's not the point. The point is W(hy)TF is this ambiguous expression so counter-intuitive? And I have an idiotic case for you at the end, so don't dismiss my answer too early (I had it on a job interview).

So, the OP's comprehension returned a list of lambdas:

[(lambda x: x*x) for x in range(10)]

This is of course just 10 different copies of the squaring function, see:

>>> [lambda x: x*x for _ in range(3)]
[<function <lambda> at 0x00000000023AD438>, <function <lambda> at 0x00000000023AD4A8>, <function <lambda> at 0x00000000023AD3C8>]

Note the memory addresses of the lambdas - they are all different!

You could of course have a more "optimal" (haha) version of this expression:

>>> [lambda x: x*x] * 3
[<function <lambda> at 0x00000000023AD2E8>, <function <lambda> at 0x00000000023AD2E8>, <function <lambda> at 0x00000000023AD2E8>]

See? 3 time the same lambda.

Please note, that I used _ as the for variable. It has nothing to do with the x in the lambda (it is overshadowed lexically!). Get it?

I'm leaving out the discussion, why the syntax precedence is not so, that it all meant:

[lambda x: (x*x for x in range(10))]

which could be: [[0, 1, 4, ..., 81]], or [(0, 1, 4, ..., 81)], or which I find most logical, this would be a list of 1 element - a generator returning the values. It is just not the case, the language doesn't work this way.

BUT What, If...

What if you DON'T overshadow the for variable, AND use it in your lambdas???

Well, then crap happens. Look at this:

[lambda x: x * i for i in range(4)]

this means of course:

[(lambda x: x * i) for i in range(4)]

BUT it DOESN'T mean:

[(lambda x: x * 0), (lambda x: x * 1), ... (lambda x: x * 3)]

This is just crazy!

The lambdas in the list comprehension are a closure over the scope of this comprehension. A lexical closure, so they refer to the i via reference, and not its value when they were evaluated!

So, this expression:

[(lambda x: x * i) for i in range(4)]

IS roughly EQUIVALENT to:

[(lambda x: x * 3), (lambda x: x * 3), ... (lambda x: x * 3)]

I'm sure we could see more here using a python decompiler (by which I mean e.g. the dis module), but for Python-VM-agnostic discussion this is enough. So much for the job interview question.

Now, how to make a list of multiplier lambdas, which really multiply by consecutive integers? Well, similarly to the accepted answer, we need to break the direct tie to i by wrapping it in another lambda, which is getting called inside the list comprehension expression:

Before:

>>> a = [(lambda x: x * i) for i in (1, 2)]
>>> a1
2
>>> a0
2

After:

>>> a = [(lambda y: (lambda x: y * x))(i) for i in (1, 2)]
>>> a1
2
>>> a0
1

(I had the outer lambda variable also = i, but I decided this is the clearer solution - I introduced y so that we can all see which witch is which).

Edit 2019-08-30:

Following a suggestion by @josoler, which is also present in an answer by @sheridp - the value of the list comprehension "loop variable" can be "embedded" inside an object - the key is for it to be accessed at the right time. The section "After" above does it by wrapping it in another lambda and calling it immediately with the current value of i. Another way (a little bit easier to read - it produces no 'WAT' effect) is to store the value of i inside a partial object, and have the "inner" (original) lambda take it as an argument (passed supplied by the partial object at the time of the call), i.e.:

After 2:

>>> from functools import partial
>>> a = [partial(lambda y, x: y * x, i) for i in (1, 2)]
>>> a0, a1
(2, 4)

Great, but there is still a little twist for you! Let's say we wan't to make it easier on the code reader, and pass the factor by name (as a keyword argument to partial). Let's do some renaming:

After 2.5:

>>> a = [partial(lambda coef, x: coef * x, coef=i) for i in (1, 2)]
>>> a0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: <lambda>() got multiple values for argument 'coef'

WAT?

>>> a[0]()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: <lambda>() missing 1 required positional argument: 'x'

Wait... We're changing the number of arguments by 1, and going from "too many" to "too few"?

Well, it's not a real WAT, when we pass coef to partial in this way, it becomes a keyword argument, so it must come after the positional x argument, like so:

After 3:

>>> a = [partial(lambda x, coef: coef * x, coef=i) for i in (1, 2)]
>>> a0, a1
(2, 4)

I would prefer the last version over the nested lambda, but to each their own...

Edit 2020-08-18:

Thanks to commenter dasWesen, I found out that this stuff is covered in the Python documentation: https://docs.python.org/3.4/faq/programming.html#why-do-lambdas-defined-in-a-loop-with-different-values-all-return-the-same-result - it deals with loops instead of list comprehensions, but the idea is the same - global or nonlocal variable access in the lambda function. There's even a solution - using default argument values (like for any function):

>>> a = [lambda x, coef=i: coef * x for i in (1, 2)]
>>> a0, a1
(2, 4)

This way the coef value is bound to the value of i at the time of function definition (see James Powell's talk "Top To Down, Left To Right", which also explains why mutable default values are shunned).

🌐
Codecademy
codecademy.com › article › python-lambda-function
Python Lambda Functions Explained (With Examples) | Codecademy
The syntax of Python’s lambda function is as follows: ... expression: This is what gets evaluated and returned. It must be a single expression with no return keyword, as it returns the result automatically. But why use lambda functions? Here are some of their key features: Anonymous: They don’t have a name unless they’ve been assigned one. Single-expression only: They can’t include loops, multiple statements, or blocks.
🌐
The Python Coding Stack
thepythoncodingstack.com › p › whats-all-the-fuss-about-python-lambda-functions
What's All the Fuss About `lambda` Functions in Python
December 1, 2023 - I'll demonstrate both scenarios: using a standard function and using a lambda function. I'll use this dictionary in the following examples: These are the scores these students got in a test. Let's figure out who passed the test. The pass mark is 5 out of 10. ... Each key in the dictionary scores is passed to check_pass(), and the function's return value is used as the output. Why are the dictionary keys passed to the function and not the key-value pairs? Iteration over dictionaries in Python uses the keys.
🌐
Real Python
realpython.com › python-lambda
How to Use Python Lambda Functions – Real Python
December 1, 2023 - Python lambdas are little, anonymous functions, subject to a more restrictive but more concise syntax than regular Python functions. Test your understanding on how you can use them better! Free Download: Get a sample chapter from Python Tricks: The Book that shows you Python’s best practices with simple examples you can apply instantly to write more beautiful + Pythonic code.
🌐
Reddit
reddit.com › r/learnpython › lambda function
r/learnpython on Reddit: Lambda function
September 9, 2023 -

I understand what the lambda function is, its an anonymous function in one line, however why using it, and what really is it? I mean every code I looked at, has it and don't forget map() reduce and filter() function are used with it, what are all these used for and why, I did my research but I still don't understand, (I have a baby's brain 🧠 y'all)

🌐
W3Schools
w3schools.com › python › gloss_python_lambda.asp
Python Lambda Function
A lambda function can take any number of arguments, but can only have one expression. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
DigitalOcean
digitalocean.com › community › tutorials › lambda-expression-python
Python Lambda Expressions Explained with Examples | DigitalOcean
July 8, 2025 - Here’s an example of using lambda with reduce to sum all elements in a list: from functools import reduce numbers = [1, 2, 3, 4, 5] sum_of_numbers = reduce(lambda x, y: x + y, numbers) print(sum_of_numbers) # Output: 15 ... Nested lambda functions in Python are lambda functions that are defined ...
🌐
Dataquest
dataquest.io › blog › tutorial-lambda-functions-in-python
Tutorial: Lambda Functions in Python
March 6, 2023 - We use a lambda function to evaluate ... we pass a lambda function as an argument to a higher-order function (the one that takes in other functions as arguments), such as Python built-in functions like filter(), map(), or reduce(). Let's look ...
🌐
freeCodeCamp
freecodecamp.org › news › lambda-function-in-python-example-syntax
Lambda Function in Python – Example Syntax
September 27, 2021 - This one is more compact, and there is not an extra function occupying space in memory. Or you could write a lambda function that checks if a number is positive, like lambda x: x > 0, and use it with filter to create a list of only positive numbers.
🌐
Scaler
scaler.com › home › topics › lambda function in python
Lambda Function in Python (with Examples) - Scaler Topics
February 2, 2024 - The following example uses the map function to double all the numbers in a list: ... Explanation: We created a list with some numbers and then used the map function to double the numbers by providing the following lambda function: lambda x: x * 2. ...
🌐
freeCodeCamp
freecodecamp.org › news › python-lambda-function-explained
How the Python Lambda Function Works – Explained with Examples
December 17, 2024 - For example, say I have a list such as [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. Now let's say that I’m only interested in those values in that list that have a remainder of 0 when divided by 2.
🌐
Python Engineer
python-engineer.com › courses › advancedpython › 08-lambda
Lambda Functions - Advanced Python 08 - Python Engineer
a = [1, 2, 3, 4, 5, 6] b = list(map(lambda x: x * 2 , a)) # However, try to prefer list comprehension # Use map if you have an already defined function c = [x*2 for x in a] print(b) print(c)
🌐
Simplilearn
simplilearn.com › home › resources › software development › learn lambda in python with syntax and examples
Learn Lambda in Python with Syntax and Examples
May 14, 2024 - Lambda in python is used to define an anonymous function in Python. Know how Python Lambda Function is used with syntax, examples, and more in this tutorial.
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States