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

🌐
Medium
medium.com › @rahulkp220 › list-comprehensions-lambda-filter-map-and-reduce-functions-in-python-d7a1bc6cd79d
List Comprehensions, lambda, filter, map and reduce functions in Python | by Rahul Lakhanpal | Medium
April 19, 2016 - >>>#Function to print the squares of any number >>> my_function = lambda x: x**2 >>> my_function(10) 100>>>#Function to print True is number greater than 10 >>> my_function = lambda x: “True” if x>10 else “False” >>> my_function(10) ‘False’ · For instance, the first parameter is a list and the second parameter is a function that has to be applied over every iterable on that list.
🌐
Stanford CS
cs.stanford.edu › people › nick › py › python-map-lambda.html
Python Map Lambda
The map() function runs a lambda function over the list [1, 2, 3, 4, 5], building a list-like collection of the results, like this:
🌐
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.
🌐
W3Schools
w3schools.com › python › python_lambda.asp
Python Lambda
Remove List Duplicates Reverse ... Syllabus Python Study Plan Python Interview Q&A Python Training ... A lambda function is a small anonymous function....
🌐
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 - A lambda definition is an expression. You can use it anywhere it applies for any exp ... Such a nice, neat explanation of this common problem. ... Woo another new thing that i didn't know. ... I don't know why you'd want to do this, exactly, but in this case the problem is that the functions' name binding rules make them use the last value available, in this case the last item in the list.
🌐
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.
Find elsewhere
🌐
Kaggle
kaggle.com › getting-started › 263209
List comprehension and Lambda function | Kaggle
# suppose you want to create a list of square numbers ranging (1-10) square_list = [(lambda x: x*x)(x) for x in range(1,10)] # alternatively square_list = [x*x for x in range(1,10)]
🌐
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.
🌐
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]
🌐
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.
🌐
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...
🌐
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....
🌐
GeeksforGeeks
geeksforgeeks.org › python-lambda-anonymous-functions-filter-map-reduce
Python Lambda Functions - GeeksforGeeks
from functools import reduce # Example: Find the product of all numbers in a list a = [1, 2, 3, 4] b = reduce(lambda x, y: x * y, a) print(b) ... The lambda multiplies two numbers at a time. ... Python Functions is a block of statements that return the specific task.
Published: December 11, 2024
🌐
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))
🌐
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 - List comprehensions can incorporate conditional expressions for more complex logic: conditional = [x if x > 2 else x**2 for x in range(5)] print(conditional)# Squares numbers less than or equal to 2, leaves others unchanged · Lambda functions can be used for intricate transformations based on conditions:
🌐
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]