It is strange how much beauty varies for different people. I find the list comprehension much clearer than filter+lambda, but use whichever you find easier.

There are two things that may slow down your use of filter.

The first is the function call overhead: as soon as you use a Python function (whether created by def or lambda) it is likely that filter will be slower than the list comprehension. It almost certainly is not enough to matter, and you shouldn't think much about performance until you've timed your code and found it to be a bottleneck, but the difference will be there.

The other overhead that might apply is that the lambda is being forced to access a scoped variable (value). That is slower than accessing a local variable and in Python 2.x the list comprehension only accesses local variables. If you are using Python 3.x the list comprehension runs in a separate function so it will also be accessing value through a closure and this difference won't apply.

The other option to consider is to use a generator instead of a list comprehension:

def filterbyvalue(seq, value):
   for el in seq:
       if el.attribute==value: yield el

Then in your main code (which is where readability really matters) you've replaced both list comprehension and filter with a hopefully meaningful function name.

Answer from Duncan on Stack Overflow
🌐
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.
Top answer
1 of 16
750

It is strange how much beauty varies for different people. I find the list comprehension much clearer than filter+lambda, but use whichever you find easier.

There are two things that may slow down your use of filter.

The first is the function call overhead: as soon as you use a Python function (whether created by def or lambda) it is likely that filter will be slower than the list comprehension. It almost certainly is not enough to matter, and you shouldn't think much about performance until you've timed your code and found it to be a bottleneck, but the difference will be there.

The other overhead that might apply is that the lambda is being forced to access a scoped variable (value). That is slower than accessing a local variable and in Python 2.x the list comprehension only accesses local variables. If you are using Python 3.x the list comprehension runs in a separate function so it will also be accessing value through a closure and this difference won't apply.

The other option to consider is to use a generator instead of a list comprehension:

def filterbyvalue(seq, value):
   for el in seq:
       if el.attribute==value: yield el

Then in your main code (which is where readability really matters) you've replaced both list comprehension and filter with a hopefully meaningful function name.

2 of 16
313

This is a somewhat religious issue in Python. Even though Guido considered removing map, filter and reduce from Python 3, there was enough of a backlash that in the end only reduce was moved from built-ins to functools.reduce.

Personally I find list comprehensions easier to read. It is more explicit what is happening from the expression [i for i in list if i.attribute == value] as all the behaviour is on the surface not inside the filter function.

I would not worry too much about the performance difference between the two approaches as it is marginal. I would really only optimise this if it proved to be the bottleneck in your application which is unlikely.

Also since the BDFL wanted filter gone from the language then surely that automatically makes list comprehensions more Pythonic ;-)

Discussions

python - lambda versus list comprehension performance - Stack Overflow
I recently posted a question using a lambda function and in a reply someone had mentioned lambda is going out of favor, to use list comprehensions instead. I am relatively new to Python. I ran a si... More on stackoverflow.com
🌐 stackoverflow.com
Efficiency of Lambda vs List comprehension
In my opinion you are asking the wrong question. Efficiency is not the reason you shouldn't be using the second one. Luckily it fails on that metric as well though: ~ $ python -m timeit '[len(w) for w in "This is a test sentence".split(" ")]' 1000000 loops, best of 3: 0.748 usec per loop ~ $ python -m timeit 'map(lambda w: len(w), "This is a test sentence".split(" "))' 1000000 loops, best of 3: 1.14 usec per loop Something I think you are missing though is that map can just use len directly. When used with a builtin, map can actually be both quite readable, and quite efficient: ~ $ python -m timeit 'map(len, "This is a test sentence".split(" "))' 1000000 loops, best of 3: 0.724 usec per loop More on reddit.com
🌐 r/learnpython
4
5
June 25, 2015
Python: list comprehensions vs. lambda - Stack Overflow
If the input list can grow large then there is a worse problem: you're iterating over the whole list, while you could stop at the first element. You could accomplish this with a for loop, but if you want to use a comprehension-like statement, here come generator expressions: More on stackoverflow.com
🌐 stackoverflow.com
python - Lambda function in list comprehensions - Stack Overflow
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. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Medium
medium.com › swlh › lambda-vs-list-comprehension-6f0c0c3ea717
Lambda vs. List Comprehension
September 22, 2021 - As the title suggests, the focus right now will be on using the Lambda function and list comprehension. But before I go on about comparing the two, I’ll have to explain how to write list comprehensions and lambda functions.
🌐
DEV Community
dev.to › suvhotta › python-lambda-and-list-comprehension-5128
Python: Lambda and List Comprehension - DEV Community
January 27, 2020 - Both Lambda Expression and List Comprehension can be handy while dealing with too many loops within the code.
Top answer
1 of 10
30

Your tests are doing very different things. With S being 1M elements and T being 300:

[x for x in S for y in T if x==y]= 54.875

This option does 300M equality comparisons.

 

filter(lambda x:x in S,T)= 0.391000032425

This option does 300 linear searches through S.

 

[val for val in S if val in T]= 12.6089999676

This option does 1M linear searches through T.

 

list(set(S) & set(T))= 0.125

This option does two set constructions and one set intersection.


The differences in performance between these options is much more related to the algorithms each one is using, rather than any difference between list comprehensions and lambda.

2 of 10
25

When I fix your code so that the list comprehension and the call to filter are actually doing the same work things change a whole lot:

import time

S=[x for x in range(1000000)]
T=[y**2 for y in range(300)]
#
#
time1 = time.time()
N=[x for x in T if x in S]
time2 = time.time()
print 'time diff [x for x in T if x in S]=', time2-time1
#print N
#
#
time1 = time.time()
N=filter(lambda x:x in S,T)
time2 = time.time()
print 'time diff filter(lambda x:x in S,T)=', time2-time1
#print N

Then the output is more like:

time diff [x for x in T if x in S]= 0.414485931396
time diff filter(lambda x:x in S,T)= 0.466315984726

So the list comprehension has a time that's generally pretty close to and usually less than the lambda expression.

The reason lambda expressions are being phased out is that many people think they are a lot less readable than list comprehensions. I sort of reluctantly agree.

🌐
Reddit
reddit.com › r/learnpython › efficiency of lambda vs list comprehension
r/learnpython on Reddit: Efficiency of Lambda vs List comprehension
June 25, 2015 -

I'm curious which the two styles of programming would result with more efficient results, whether it's measured by space or speed.

Let's say I wanted to print a list of the lengths of each word in a sentence, which of the following to methods would be more efficient:

print [len(w) for w in "This is a test sentence".split(" ")] print map(lambda w: len(w), "This is a test sentence".split(" "))

🌐
Quora
quora.com › Is-it-better-to-use-list-comprehensions-or-map-with-a-lambda-function-when-manipulating-elements-in-a-list
Is it better to use list comprehensions or map with a lambda function when manipulating elements in a list? - Quora
Answer (1 of 3): Here are my rules of thumb: * When all you’re doing is calling an already-defined function on each element, [code ]map(f, lst)[/code] is a little faster than the corresponding list comprehension [code ][f(x) for x in lst][/code]. To an experienced Python programmer, I think ...
Find elsewhere
🌐
Python Guides
pythonguides.com › python-list-comprehension
Lambda in List Comprehension in Python
September 5, 2025 - A lambda function in Python is a small, anonymous function. It doesn’t need a name and is usually written in one line. ... Now, let’s see how we can combine this with list comprehensions.
🌐
LinkedIn
linkedin.com › pulse › list-comprehension-lambda-functions-leonardo-a
List Comprehension & Lambda Functions | Leonardo A.
March 24, 2021 - list(map(lambda num: num**2, array1)) ... setting the square function, we are doing this at inline runtime. List Comprehension has better visibility and readability....
🌐
Practity
practity.com › home › list comprehension and lambda functions
List comprehension. Python Tutorials. Practity
November 28, 2023 - Quick tutorial to understand list comprehension in Python. Find out how to use them and the difference with the lambda function.
Top answer
1 of 2
11

When the list is so small there is no significant difference between the two. If the input list can grow large then there is a worse problem: you're iterating over the whole list, while you could stop at the first element. You could accomplish this with a for loop, but if you want to use a comprehension-like statement, here come generator expressions:

# like list comprehensions but with () instead of []
gen = (b for a, b in foo if a == 'b')
my_element = next(gen)

or simply:

my_element = next(b for a, b in foo if a == 'b')

If you want to learn more about generator expressions give a look at PEP 289.


Note that even with generators and iterators you have more than one choice.

# Python 3:
my_element = next(filter(lambda x: x[0] == 'b', foo))

# Python 2:
from itertools import ifilter
my_element = next(ifilter(lambda (x, y): x == 'b', foo))

I personally don't like and don't recommend this because it is much less readable. It turns out that this is actually slower than my first snippet, but more in general using filter() instead of a generator expression might be faster in some special cases.

In any case if you need benchmarking your code, I recommend using the timeit module.

2 of 2
4

This is slower than DavidE's answer (I timed it), but has the advantage of simplicity:

z = dict(foo)['b']

Of course it assumes that your keys are all hashable, but that's fine if they are strings. If you need to do multiple lookups though this is definitely the way to go (just be sure to only convert to a dict once).

🌐
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 - To start with, List comprehension provide a “shorthand” way to create lists. Didn’t get me? Ohk, how will you print 10 numbers starting from 0 in the form of a list?
🌐
3Ri Technologies
3ritechnologies.com › python-list-function-or-list-comprehension
Python List Function vs. List Comprehension: Which to Use? | 3ritechnologies
June 6, 2025 - This code generates the square_function lambda function, which accepts an integer as input and returns the square of that number. Although the lambda function is more straightforward to read and comprehend than list comprehension in the preceding ...
🌐
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...
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).

🌐
SciPython
scipython.com › books › book2 › chapter-4-the-core-python-language-ii › questions › list-comprehension-and-lambda-functions
Q4.3.1: List comprehension and lambda functions
creates the same list of anonymous functions as that in Example E4.11. Note that we need to pass each i in to the lambda function explicitly or else Python's closure rules will lead to every lambda function being equivalent to x**3 (3 being the final value of i in the loop).
🌐
Devgex
devgex.com › en › article › 00003072
Comparative Analysis of List Comprehension vs. filter+lambda in Python: Performance and Readability - DevGex
November 1, 2025 - List comprehension employs an intuitive declarative syntax: xs = [x for x in xs if x.attribute == value]. This approach embeds filtering conditions directly within the list construction process, making code logic immediately apparent. In contrast, the filter+lambda combination: xs = filter(lambda x: x.attribute == value, xs) adopts a functional programming paradigm, encapsulating filtering logic within lambda functions.
🌐
PhoenixNAP
phoenixnap.com › home › kb › devops and development › list comprehension in python
List Comprehension in Python {Benefits, Examples}
June 5, 2025 - Lambda functions are a shorthand way to write a Python function without a name. They have a broader application than list comprehensions, as they can output values other than lists.
🌐
Programiz
programiz.com › python-programming › list-comprehension
Python List Comprehension (With Examples)
While list comprehension is commonly used for filtering a list based on some conditions, lambda functions are commonly used with functions like map() and filter().
🌐
Switowski
switowski.com › blog › map-vs-list-comprehension
map() vs. List Comprehension - Sebastian Witowski
July 31, 2023 - This time list comprehension is around 44% slower than map() (45.4/31.5≈1.44). map() used with a lambda function is usually slower than the equivalent list comprehension.