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

๐ŸŒ
Stanford CS
cs.stanford.edu โ€บ people โ€บ nick โ€บ py โ€บ python-map-lambda.html
Python Map Lambda
To work with map(), the lambda should have one parameter in, representing one element from the source list. Choose a suitable name for the parameter, like n for a list of numbers, s for a list of strings. The result of map() is an "iterable" map object which mostly works like a list, but it ...
๐ŸŒ
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 - See https://www.reddit.com/r/learnpython/comments/zajla6/how_to_return_a_list_of_lambda_function/iyn8wqq/ ... Interesting. I always mentally translated pwd to print working directory. ... One more way is to use map and a lambda that returns another lambda.
๐ŸŒ
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
In this tutorial, we will explore how to apply lambda functions to common list operations, providing practical examples and use cases to enhance your Python programming skills.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ difference-between-list-comprehension-and-lambda-in-python
List comprehension and Lambda Function in Python - GeeksforGeeks
July 15, 2025 - List Comprehension is used to create lists, Lambda is function that can process like other functions and thus return values or lists.
๐ŸŒ
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...
Find elsewhere
๐ŸŒ
Medium
medium.com โ€บ @emoome82 โ€บ advanced-list-comprehensions-and-lambda-functions-in-python-a-deep-dive-0baf5d2b8622
Advanced List Comprehensions and Lambda Functions in Python: A Deep Dive | by civilpy | Medium
April 23, 2024 - This blog post will explore the use of nested list comprehensions, conditional list comprehensions, list comprehensions with multiple iterables, lambda functions, lambda functions within list comprehensions, list comprehensions for flattening lists, applying functions to elements, lambda with map and filter, conditional expressions within list comprehensions, and complex transformations with lambda functions.
๐ŸŒ
DEV Community
dev.to โ€บ suvhotta โ€บ python-lambda-and-list-comprehension-5128
Python: Lambda and List Comprehension - DEV Community
January 27, 2020 - However in python 3 it is a map object. To get a list, built-in list() function can be used : list(map(func,iterables)) Combining map and Lambda: Suppose we've a mundane task of increasing all the elements of a list by 3 units.
๐ŸŒ
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....
๐ŸŒ
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.
๐ŸŒ
Python Examples
pythonexamples.org โ€บ python-lambda-function-with-list-comprehension
Python Lambda Function with List Comprehension
In this example, we shall define a lambda function that takes a list of strings myList as argument, use list compression to find the length of each string in the list, and return the resulting list. len_of_strings = lambda names: [len(x) for x in names] names = ['apple', 'banana', 'fig'] lengths = len_of_strings(names) print(lengths) ... In this tutorial of Python Lambda Function, we learned how to define a lambda function that does list compression, and returns the resulting list, with the help of example programs.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ python-lambda-function-to-check-if-a-value-is-in-a-list
Python - Lambda Function to Check if a value is in a List
July 18, 2023 - Map method on the other hand is an in?built method in Python which allows us to apply any specific function to all the elements of the iterable object. It takes the function and the iterable object as the parameters. In the following example we have used the 'any', 'map', and the 'lambda' function to check if a value exists in a list.
๐ŸŒ
CloudxLab
cloudxlab.com โ€บ assessment โ€บ displayslide โ€บ 4981 โ€บ lambda-function-and-list-comprehension
Lambda Function and List Comprehension | Automated hands-on| CloudxLab
Using lambda with map In the below example, we double the value of each element. doubled_list = map(lambda x: x*2, foo) print(list(doubled_list))
๐ŸŒ
Quora
quora.com โ€บ How-to-use-lambda-function-with-lists-in-python-3
How to use lambda function with lists in python 3? - Quora
Answer (1 of 4): Python 2.7.10 A=[1,2,3,4] square = map(lambda n: n**2,A) print square o/p:[1, 4, 9, 16] ______________________________________ Python 3.x A=[1,2,3,5] sqr =map(lambda n: n**2,A) print (list(sqr)) o/p: [1, 4, 9, 25]
๐ŸŒ
Kaggle
kaggle.com โ€บ getting-started โ€บ 263209
List comprehension and Lambda function | Kaggle
The use of list comprehension and lambda function can make your code concise and smaller. so let's have a look on these. List comprehension List comprehensio...
๐ŸŒ
pythoncodelab
pythoncodelab.com โ€บ home โ€บ how to use lambda function in list comprehension in python
How To Use Lambda Function In List Comprehension In Python -
November 9, 2024 - However, the lambda function begins with the lambda keyword followed by the input parameters. ... A parameter list is a list of parameters for lambda functions separated by a comma.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_lambda.asp
Python Lambda
Remove List Duplicates Reverse ... Python Interview Q&A Python Bootcamp Python Training ... A lambda function is a small anonymous function....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-lambda-anonymous-functions-filter-map-reduce
Python Lambda Functions - GeeksforGeeks
The function is called with a lambda function and a new list is returned which contains all the lambda-modified items returned by that function for each item. ... The lambda function doubles each number. map() iterates through a and applies ...
Published ย  December 11, 2024