🌐
Caisbalderas
caisbalderas.com › blog › iterating-with-python-lambdas
Iterating With Python Lambdas - Carlos Isaac Balderas
You'll see that the difference ... A slightly more difficult example. The for loop representation is straightforward; iterate over x, multiply the odd values by 5 and add them to the list y....
🌐
EyeHunts
tutorial.eyehunts.com › home › python lambda for loop | example code
Python lambda for loop | Example code - Tutorial - By EyeHunts
May 30, 2022 - Answer: Simply create a list of lambdas in a python loop using the following code. def square(x): return lambda: x * x lst = [square(i) for i in [1, 2, 3, 4, 5]] for f in lst: print(f())
Discussions

python - loop for inside lambda - Stack Overflow
Just wanted to add one more that ... to use lambda just for the sake of making it a one line code). Instead, you can use a simple list comprehension. ... BTW, the return values will be a list of Nones. ... Save this answer. ... Show activity on this post. Since a for loop is a statement (as is print, in Python 2.x), you ... More on stackoverflow.com
🌐 stackoverflow.com
Python - Iterating through a list -Lambda - Stack Overflow
Below I have created a function which iterates through a list of numbers and selects the last 3 digits from each numbers and puts them as new numbers in another list.I also sort them from smallest to More on stackoverflow.com
🌐 stackoverflow.com
python - Lambda function in list comprehensions - Stack Overflow
What if you DON'T overshadow the for variable, AND use it in your lambdas??? Well, then crap happens. Look at this: ... 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! ... I'm sure we could see more here using a python ... More on stackoverflow.com
🌐 stackoverflow.com
November 5, 2017
python - Creating lambda inside a loop - Stack Overflow
Copylambdas_list = [ lambda i=o: i.some_var for o in obj_list ] ... In both cases the key is to make sure each value in the obj_list list is assigned to a unique scope. Your solution didn't work because the lexical variable obj is referenced from the parent scope (the for). More on stackoverflow.com
🌐 stackoverflow.com
🌐
OpenGenus
iq.opengenus.org › python-lambda-for-loop
Python lambda for loop
November 27, 2022 - list = [91, 12, 63, 5] f = lambda listx: [print(x) for x in listx] f(list) ... The output is as expected. Moving forward, you may want to replace a for loop which a corresponding lambda (without using for loop). This is also possible either using the lambda recursively or using reduce built-in ...
🌐
Finxter
blog.finxter.com › home › learn python blog › python one line for loop lambda
Python One Line For Loop Lambda - Be on the Right Side of Change
July 23, 2020 - The result will be a new list resulting from evaluating the expression in the context of the for and if clauses which follow it.”Official Python Documentation · Here is the formula for list comprehension. That’s the one thing you should take home from this tutorial. Formula: List comprehension consists of two parts. ... The first part is the expression. In the example above it was the variable x. But you can also use a more complex expression such as x.upper(). Use any variable in your expression that you have defined in the context within a loop statement.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-iterating-with-python-lambda
Python: Iterating With Python Lambda - GeeksforGeeks
July 23, 2025 - Then, we do to the square of it ... to iterate over list l1 # filter is used to find odd numbers l2 = list(map(lambda v: v ** 2, filter(lambda u: u % 2, l1))) # print list print(l2)...
🌐
TutorialsPoint
tutorialspoint.com › How-to-create-a-lambda-inside-a-Python-loop
How to create a lambda inside a Python loop?
# This creates a common pitfall list_of_lambdas = [] for i in range(1, 6): list_of_lambdas.append(lambda: i*i) # All lambdas use the final value of i for f in list_of_lambdas: print(f()) ... Use default parameters lambda i=i: i*i for simple cases or helper functions for complex logic. Avoid creating lambdas directly in regular loops without proper variable capture.
Find elsewhere
🌐
IncludeHelp
includehelp.com › python › iterating-with-python-lambda.aspx
Iterating with Python Lambda
# Python program to demonstrate the example to # iterate with lambda # list of integers numbers = [10, 15, 20, 25, 30] # list to store cubes cubes = [] # for loop to iterate over list for x in numbers: # lambda expression to find cubes res = lambda x: x**3 # appending to the list (cubes) cubes.append(res(x)) # print the lists print("numbers:", numbers) print("cubes:", cubes)
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
190

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

🌐
Python documentation
docs.python.org › 3 › tutorial › controlflow.html
4. More Control Flow Tools — Python 3.14.7 documentation
Rather than always iterating over an arithmetic progression of numbers (like in Pascal), or giving the user the ability to define both the iteration step and halting condition (as C), Python’s for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence. For example (no pun intended): >>> # Measure some strings: >>> words = ['cat', 'window', 'defenestrate'] >>> for w in words: ... print(w, len(w)) ... cat 3 window 6 defenestrate 12 · Code that modifies a collection while iterating over that same collection can be tricky to get right. Instead, it is usually more straight-forward to loop over a copy of the collection or to create a new collection:
🌐
Finxter
blog.finxter.com › home › learn python blog › python for loop inside lambda
Python For Loop Inside Lambda - Be on the Right Side of Change
June 13, 2021 - There are two ways of doing this: x = [2, 3, 4, 5, 6] y = [v * 5 for v in x if v % 2] print(y) # [15, 25] x = [2, 3, 4, 5, 6] y = list(map(lambda v: v * 5, filter(lambda u: u % 2, x))) print(y) # [15, 25]
🌐
DEV Community
dev.to › rossli › way-to-better-explain-python-lambda-in-for-loop-1mp8
Way to BETTER explain Python lambda in for loop. - DEV Community
November 6, 2023 - Credit: Thanks to YouTube channel mCoding for showing the internals of Python list comprehensions! increment_by_i = [lambda x: x + i for i in range(5)] is equalivent to: increment_by_i = list(lambda x: x + i for i in range(5)), where lambda(x: x + i for i in range(10)) is a generator expression, which is equalivent to:
🌐
Reddit
reddit.com › r/learnpython › issue with creating lambda functions using for loop
r/learnpython on Reddit: Issue with creating lambda functions using for loop
February 7, 2022 -

Hey all,

Can someone explain why when I run the below code:

arr = []
for n in range(1,5):
    arr.append(lambda n: 5+n)

I can't run one of the functions in the array by simply calling arr[0]()with empty parenthesis?

it throws an error saying I need to pass one positional argument, n - but I'm passing it using for loop, am I not?

There is evidently something I'm missing.

What makes it worse to me is that ok, if I pass an argument, say I will call art[0](7) - that makes the n in my lambda and in for loop useless, because the value (of n) didn't get passed at all to that lambda n: definition...

Any help would be appreciated!

Top answer
1 of 5
7

Lambdas are just another way of defining a function

def foo(x):
    return x + x

is the same as

foo = lambda x: x + x

So let's start with a function to do what you want:

def first_missing(items, base):
    for number in itertools.count():
        text = base + '_' + str(number)
        if text not in items:
             return text

The first thing to note is that you can't use loops inside a lambda. So we'll need to rewrite this without a loop. Instead, we'll use recursion:

def first_missing(items, base, number = 0):
        text = base + '_' + str(number)
        if text not in items:
             return text
        else:
             return first_missing(items, base, number + 1)

Now, we also can't use an if/else block in a lambda. But we can use a ternary expression:

def first_missing(items, base, number = 0):
        text = base + '_' + str(number)
        return text if text not in items else first_missing(items, base, number + 1)

We can't have local variables in a lambda, so we'll use a trick, default arguments:

def first_missing(items, base, number = 0):
        def inner(text = base + '_' + str(number)):
            return text if text not in items else first_missing(items, base, number + 1)
        return inner()

At this point we can rewrite inner as a lambda:

def first_missing(items, base, number = 0):
        inner = lambda text = base + '_' + str(number): text if text not in items else first_missing(items, base, number + 1)
        return inner()

We can combine two lines to get rid of the inner local variable:

def first_missing(items, base, number = 0):
    return (lambda text = base + '_' + str(number): text if text not in items else first_missing(items, base, number + 1))()

And at long last, we can make the whole thing into a lambda:

first_missing = lambda: items, base, number = 0: (lambda text = base + '_' + str(number): text if text not in items else first_missing(items, base, number + 1))()

Hopefully that gives you some insight into what you can do. But don't ever do it because, as you can tell, lambdas can make your code really hard to read.

2 of 5
3

There's no need to use a lambda in this case, a simple for loop will do:

my_test  = 'test_name_dup'  
testlist = ['test_name', 'test_name_dup','test_name_dup_1', 'test_name_dup_3']

for i in xrange(1, len(testlist)):
    if my_test + '_' + str(i) not in testlist:
        break

print my_test + '_' + str(i)
> test_name_dup_2

If you really, really want to use a lambda for this problem, you'll also have to learn about itertools, iterators, filters, etc. I'm gonna build on thg435's answer, writing it in a more idiomatic fashion and explaining it:

import itertools as it

iterator = it.dropwhile(
    lambda n: '{0}_{1}'.format(my_test, n) in testlist,
    it.count(1))

print my_test + '_' + str(iterator.next())
> test_name_dup_2

The key to understanding the above solution lies in the dropwhile() procedure. It takes two parameters: a predicate and an iterable, and returns an iterator that drops elements from the iterable as long as the predicate is true; afterwards, returns every element.

For the iterable, I'm passing count(1), an iterator that produces an infinite number of integers starting from 1.

Then dropwhile() starts to consume the integers until the predicate is false; this is a good opportunity for passing an in-line defined function - and here's our lambda. It receives each generated integer in turn, checking to see if the string test_name_dup_# is present in the list.

When the predicate returns false, dropwhile() returns and we can retrieve the value that made it stop by calling next() on it.

🌐
LearnPython.com
learnpython.com › blog › python-list-loop
7 Ways to Loop Through a List in Python | LearnPython.com
Let’s see how to use lambda as we loop through a list. We’ll make a for loop to iterate over a list of numbers, find each number's square, and save or append it to the list. Finally, we’ll print a list of squares.