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
Top answer
1 of 7
371

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
192

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?

>>> a0
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).

🌐
Reddit
reddit.com › r/learnpython › how to return a list of lambda function?
How to return a list of lambda function? : r/learnpython
December 2, 2022 - That's because your lambda function includes the variable i that it gets from the environment. Trouble is, all the lambda functions share the same environment so for each function i has the last value assigned to it which is 3. One way around the problem is to force the i value to be passed as a parameter to the lambda which "preserves" the i value as the value when the lambda was actually defined: list1 = [1, 2, 3] pwd = [lambda x, i=i: x**i for i in list1] print(pwd) for func in pwd: print(func(2))
Discussions

How to create a list of different lambda functions.
[(lambda x, i=i: x*i) for i in range(3)] https://docs.python.org/3/faq/programming.html#why-do-lambdas-defined-in-a-loop-with-different-values-all-return-the-same-result More on reddit.com
🌐 r/learnpython
13
70
February 13, 2021
python - How do I create a list of lambdas (in a list comprehension/for loop)? - Stack Overflow
When function statements are executed they are bound to their (lexically) enclosing scope. In your snippet, the lambdas are bound to the global scope, because for suites are not executed as an independently scoped unit in Python. At the end of the for loop, the num is bound in the enclosing scope. More on stackoverflow.com
🌐 stackoverflow.com
How to return a list of lambda function?
That's because your lambda function includes the variable i that it gets from the environment. Trouble is, all the lambda functions share the same environment so for each function i has the last value assigned to it which is 3. One way around the problem is to force the i value to be passed as a parameter to the lambda which "preserves" the i value as the value when the lambda was actually defined: list1 = [1, 2, 3] pwd = [lambda x, i=i: x**i for i in list1] print(pwd) for func in pwd: print(func(2)) More on reddit.com
🌐 r/learnpython
19
87
December 2, 2022
Multiple conditions in python filter function
No, why would you expect your output to include 2 when your condition explicitly says x!=2? The other condition excludes any even number, since an odd number modulus 2 returns 1, and 1 converts to True. 0 converts to False. So, you're explicitly filtering out any number evenly divisible by 2. More on reddit.com
🌐 r/learnprogramming
4
1
January 7, 2019
🌐
Python Guides
pythonguides.com › python-list-comprehension
Lambda in List Comprehension in Python
September 5, 2025 - In this tutorial, I’ll show you exactly how I use lambda inside list comprehensions. I’ll cover filtering, mapping, and real-world use cases. ... A lambda function in Python is a small, anonymous function.
🌐
W3Schools
w3schools.com › python › python_lambda.asp
Python Lambda
Remove List Duplicates Reverse ... Python Study Plan Python Interview Q&A Python Training ... A lambda function is a small anonymous function....
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-lambda-anonymous-functions-filter-map-reduce
Python Lambda Functions - GeeksforGeeks
Explanation: lambda function multiplies each element by 10. List comprehension iterates through range(1, 5) and applies the lambda to each value. 3. Returning Multiple Results: Although a lambda can contain only one expression, it can still ...
Published: May 18, 2026
🌐
LabEx
labex.io › tutorials › python-how-to-apply-lambda-in-list-operations-434458
How to apply lambda in list operations | LabEx
This tutorial explores the versatile ... functions provide a concise and powerful way to perform complex transformations, filtering, and manipulations on lists without writing traditional function definitions....
🌐
LabEx
labex.io › tutorials › python-how-to-use-lambda-functions-for-simple-list-operations-in-python-415576
How to use lambda functions for simple list operations in Python | LabEx
Lambda functions, also known as anonymous functions, are small, one-line functions in Python that can be defined without a name. They are typically used for simple, short-lived operations where a full-fledged function definition is not necessary.
Find elsewhere
🌐
Stanford CS
cs.stanford.edu › people › nick › py › python-map-lambda.html
Python Map Lambda
Lambda is perfect where you have a short computation to write inline. Many programs have some sub-part which can be solved very compactly this way. For longer code, def is better. The map() function runs a lambda function over the list [1, 2, 3, 4, 5], building a list-like collection of the ...
🌐
Real Python
realpython.com › python-lambda
How to Use Python Lambda Functions – Real Python
June 14, 2025 - As in any programming languages, you will find Python code that can be difficult to read because of the style used. Lambda functions, due to their conciseness, can be conducive to writing code that is difficult to read. The following lambda example contains several bad style choices: ... >>> (lambda _: list(map(lambda _: _ // 2, _)))([1,2,3,4,5,6,7,8,9,10]) [0, 1, 1, 2, 2, 3, 3, 4, 4, 5]
🌐
Quora
quora.com › How-to-use-lambda-function-with-lists-in-python-3
How to use lambda function with lists in python 3? - Quora
Quora is a place to gain and share knowledge. It's a platform to ask questions and connect with people who contribute unique insights and quality answers.
🌐
DEV Community
dev.to › suvhotta › python-lambda-and-list-comprehension-5128
Python: Lambda and List Comprehension - DEV Community
January 27, 2020 - One can make most out of these Lambda expressions by using it with map or filter functions. Syntax: map(func,iterables) map() passes each element in the iterable through a function and returns the result of all elements having passed through the function. func is the function which would be applied on each element present in iterables. The return type of map() function is a list in python 2.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Lambda Expressions in Python | note.nkmk.me
August 19, 2023 - Filter (extract/remove) items of a list with filter() in Python · When specifying a function (callable object) as an argument, using a lambda expression is often simpler than defining the function with a def statement.
🌐
GeeksforGeeks
geeksforgeeks.org › difference-between-list-comprehension-and-lambda-in-python
List comprehension and Lambda Function in Python | GeeksforGeeks
October 13, 2024 - The following are some of the characteristics of Python lambda functions: A lambda function can take more than one num ... List comprehension is an elegant way to define and create a list in Python.
🌐
freeCodeCamp
freecodecamp.org › news › python-lambda-functions
Python Lambda Functions – How to Use Anonymous Functions with Examples
February 24, 2023 - The sorted function uses this lambda function to extract the "age" value for each dictionary in the employees list and uses these values as the sort keys. In addition to the sorted function, many other functions in Python can take a key argument, including the max, min, and sorted functions.
🌐
Kaggle
kaggle.com › getting-started › 263209
List comprehension and Lambda function | Kaggle
days = [1,2,3,4,5,6,7] new_days_list = [num for num in days if num % 2 == 0] print(new_days_list) #this can be written without a list new_days_list = [num for num in range(1,8) if num % 2 == 0] ... Lambda is a small anonymous function.
🌐
ScholarHat
scholarhat.com › home › tutorials › python › lambda function in python..
Lambda Function in Python with Examples (Full Tutorial)
September 10, 2025 - Here we have done Condition Checking Using the Python lambda function. In the above program, the ‘format_numric’ calls the lambda function, and the num is passed as a parameter to perform operations. We are generating a new lambda function with a default argument of x (the current item in the iteration) on each iteration within the list comprehension.
🌐
Quora
quora.com › How-do-you-approach-list-comprehension-with-lambda-Python-lambda-list-comprehension-development
How to approach list comprehension with lambda (Python, lambda, list comprehension, development) - Quora
Answer: I usually approach it by not trying to use them together in the first place. One of the points of a comprehension is that you can just use an expression directly for the transformed value or the test, unlike a map or filter call. For example, these all do the same thing: [code]>>> valu...
🌐
Dataquest
dataquest.io › home › blog › how to use a lambda function in python
Lambda Functions in Python (With Examples)
April 21, 2026 - If you need multiple lines of logic, ... function with def. Use a lambda when you need a short, one-time function that you'll pass directly as an argument, like sorting with a custom key or filtering a list....