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

🌐
Finxter
blog.finxter.com › home › learn python blog › python lists filter() vs list comprehension – which is faster?
Python Lists filter() vs List Comprehension - Which is Faster? - Be on the Right Side of Change
April 17, 2020 - For large lists with one million elements, filtering lists with list comprehension is 40% faster than the built-in filter() method. To answer this question, I’ve written a short script that tests the runtime performance of filtering large ...
Discussions

map, filter vs list comprehension. Which is faster?

Currently, in the program that I am working on, lists never have more than 50 items at any given time.

The easy answer: I doubt you care at all about the question in the title.

More on reddit.com
🌐 r/learnpython
6
1
March 1, 2022
Diffenence between List Comprehension and Map/Filter?
In Haskell, a list comprehension is just a syntactical form that allows you to combine a generator (an anamorphism) , a mapping or folding function, and a filter in a fairly expressive single-liner. From this standpoint, a list comprehension could be seen as a specialized syntax to combine high order functions that consume the results of a generator and that generator. More on reddit.com
🌐 r/functionalprogramming
13
10
September 24, 2023
python - When would using the filter function be used instead of a list comprehension? - Stack Overflow
List comprehensions are for conveniently expressing mapping/filtering operations. For what it's worth, Guido wanted to remove map and filter because the language had comprehensions. ... Different versions of python had different style preferences and performance characteristics for comprehensions ... More on stackoverflow.com
🌐 stackoverflow.com
python - Confusion about efficiencies after evaluating list comprehension performance - Stack Overflow
In Python 2 you could see if itertools.ifilter makes any difference. ... Have you tried doing performance analysis? Docs for profiling are here and there is an existing SO question here ... Haven't heard of it, yet. But looks very very useful to me, I will use it to do a more comprehensive analysis/check in the next couple of days. ... In the filter ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Medium
medium.com › swlh › python-list-comprehensions-vs-map-list-filter-functions-ebf0f8efe0e9
Python List Comprehensions vs. Map/List/Filter Functions | by Jonathan Craig | The Startup | Medium
November 8, 2020 - For me, in my world, this makes functions like map() and filter() functions worthless. I can achieve the same performance with a list comprehension and gain all of the value of a list object without ever being forced to call list() in order to be able to access specific indexes or slices of indexes.
🌐
Switowski
switowski.com › blog › for-loop-vs-list-comprehension
For Loop vs. List Comprehension - Sebastian Witowski
September 17, 2020 - $ python -m timeit -s "from filter_list import filter_return_list" "filter_return_list()" 2 loops, best of 5: 104 msec per loop · Now, its performance is not so great anymore. It's 133% slower than the list comprehension (104/44.5≈2.337) ...
🌐
Recology
recology.info › list comprehension vs. filter vs. key lookup
List comprehension vs. filter vs. key lookup | Recology
April 18, 2022 - First, I naively started off with using filter(). When that lead to waiting more than ten minutes, I read that list comprehensions are faster.
🌐
Quora
quora.com › Is-it-better-to-use-list-comprehensions-or-filter-lambda-in-Python
Is it better to use list comprehensions or filter() +lambda in Python? - Quora
Answer (1 of 2): LIST COMPREHENSION [code]even = [i for i in range(101) if i%2==0] print(even) [/code]FILTER + LAMBDA [code]even = filter(lambda n : n%2 == 0, range(101)) print(list(even)) [/code]If you want the code which is faster, I would tell you to choose the filter() + lambda. It is the f...
🌐
Python
mail.python.org › pipermail › python-list › 2001-December › 096256.html
list comprehension performance vs. map() and filter()
September 25, 2013 - And the winner is... well, it depends. map() and filter() pros: - Inner loop is C instead of Python bytecode. (Negligible.) - map() predicts the size of the result list and allocates it all at once. List comprehensions use list.append() instead. - map(F, L) only performs the name lookup for F once.
Find elsewhere
🌐
EyeHunts
tutorial.eyehunts.com › home › python filter vs list comprehension | difference
Python Filter vs List Comprehension | Difference
June 9, 2022 - List comprehension is easier to read, understand, and type. ... Answer: When the list is so small there is no significant difference between the two. But if you want the code which is faster, I would tell you to choose the filter() + lambda. It is the faster one · Comment section code(François ...
🌐
Devgex
devgex.com › en › article › 00003072
Comparative Analysis of List Comprehension vs. filter+lambda in Python: Performance and Readability - DevGex
November 1, 2025 - Practical testing demonstrates that for lists containing 10,000 elements, list comprehension outperforms filter+lambda by approximately 30-40%. While this difference appears negligible with small datasets, it accumulates into significant performance ...
🌐
sqlpey
sqlpey.com › python › python-list-comprehension-vs-filter-debate
Python List Comprehension Versus Filter: Performance and Pythonic Style Debate
November 4, 2025 - If the goal is merely to filter, list comprehensions are often cited as the more “Pythonic” idiom, particularly since the BDFL considered removing filter from Python 3 built-ins (though it was ultimately moved to functools). Performance differences are often negligible for small datasets but can become relevant during intensive profiling.
🌐
Switowski
switowski.com › blog › map-vs-list-comprehension
map() vs. List Comprehension - Sebastian Witowski
July 31, 2023 - I concluded that, while filter() ... memory-efficient generator object that the filter() function returns), list comprehension is usually the faster choice....
🌐
YouTube
youtube.com › watch
List comprehension vs filter in Python (speed, lambda, history, examples) - YouTube
Python tutorial on the difference between the filter() function and list comprehensions. We learn about using filter() with lambda and list comprehensions wi...
Published   May 24, 2020
🌐
sqlpey
sqlpey.com › python › python-performance-comprehension-vs-loops
Python: List Comprehension vs. For Loops vs. Map/Filter/Reduce Performance
November 4, 2025 - However, this advantage diminishes, and can even reverse, if the list comprehension is used to build a list that is then immediately discarded. Functional programming tools like map, filter, and reduce are typically implemented in C, which can provide a speed advantage. Yet, when used with Python lambda functions, the overhead of repeatedly setting up Python stack frames can negate some of these C-level gains. Often, performing the operations directly within a list comprehension or a straightforward for loop can be marginally faster than using map with a lambda.
🌐
Medium
medium.com › @kjbyun0 › comprehension-and-its-performance-in-python-ac79da107e82
Comprehension and its performance in Python | by Kjbyun | Medium
February 12, 2024 - Please, note map/filter function returns a map/filter object which is also an iterator, and, if a map/filter is called with an already defined function as one of its arguments, it is generally faster than its list comprehension alternative.
🌐
Plain English
python.plainenglish.io › incredibly-fast-ways-to-filter-lists-in-python-76fce2e06cd5
Incredibly Fast Ways to Filter Lists in Python | by Naveen Pandey | Python in Plain English
September 15, 2023 - In this blog post, we will explore two popular and efficient methods for filtering lists in Python: list comprehensions and the filter…
🌐
Reddit
reddit.com › r/functionalprogramming › diffenence between list comprehension and map/filter?
r/functionalprogramming on Reddit: Diffenence between List Comprehension and Map/Filter?
September 24, 2023 -

Is there any significant difference between List Comprehensions like in Python or JavaScript and the Higher Order Functions "Map" and "Filter" in functional languages?

It seems that both take a list and return a new list. It's just a different syntax, like this example in Python

squares = [x**2 for x in numbers]
squares = list(map(lambda x: x**2, numbers))

Semantically, they are identical. They take a list of elements, apply a function to each element, and return a new list of elements.

The only difference I've noticed is that higher order functions can be faster in languages like Haskell because they can be optimized and run on multiple cores.

Edit: ChatGPT gave me a hint about the differences. Is that correct?

Semantically, List Comprehensions and the map and filter functions actually have some similarities, since they are all used to perform transformations on elements of a list and usually return a new list. These similarities can lead to confusion, as they seem similar at first glance. Let's take a closer look at the semantics:

Transformation:

List Comprehensions: you use an expression to transform each element of the source list and create a new list with the transformed values.

Map: It applies a specified function to each element of the source list and returns a list with the transformed values.

Filtering: List Comprehensions: you can insert conditions in List Comprehensions to select or filter elements based on a condition.

filter: This function is used specifically to select elements from the source list that satisfy a certain condition.

The semantic differences are therefore:

List Comprehensions can combine both transformations and filtering in a single construction, making their syntax versatile.

map is restricted to transformations and creates a new list of transformed values.

filter specializes in selecting elements based on a condition and returns a list with the selected elements.

Top answer
1 of 9
221

The following are rough guidelines and educated guesses based on experience. You should timeit or profile your concrete use case to get hard numbers, and those numbers may occasionally disagree with the below.

A list comprehension is usually a tiny bit faster than the precisely equivalent for loop (that actually builds a list), most likely because it doesn't have to look up the list and its append method on every iteration. However, a list comprehension still does a bytecode-level loop:

>>> dis.dis(<the code object for `[x for x in range(10)]`>)
 1           0 BUILD_LIST               0
             3 LOAD_FAST                0 (.0)
       >>    6 FOR_ITER                12 (to 21)
             9 STORE_FAST               1 (x)
            12 LOAD_FAST                1 (x)
            15 LIST_APPEND              2
            18 JUMP_ABSOLUTE            6
       >>   21 RETURN_VALUE

Using a list comprehension in place of a loop that doesn't build a list, nonsensically accumulating a list of meaningless values and then throwing the list away, is often slower because of the overhead of creating and extending the list. List comprehensions aren't magic that is inherently faster than a good old loop.

As for functional list processing functions: While these are written in C and probably outperform equivalent functions written in Python, they are not necessarily the fastest option. Some speed up is expected if the function is written in C too. But most cases using a lambda (or other Python function), the overhead of repeatedly setting up Python stack frames etc. eats up any savings. Simply doing the same work in-line, without function calls (e.g. a list comprehension instead of map or filter) is often slightly faster.

Suppose that in a game that I'm developing I need to draw complex and huge maps using for loops. This question would be definitely relevant, for if a list-comprehension, for example, is indeed faster, it would be a much better option in order to avoid lags (Despite the visual complexity of the code).

Chances are, if code like this isn't already fast enough when written in good non-"optimized" Python, no amount of Python level micro optimization is going to make it fast enough and you should start thinking about dropping to C. While extensive micro optimizations can often speed up Python code considerably, there is a low (in absolute terms) limit to this. Moreover, even before you hit that ceiling, it becomes simply more cost efficient (15% speedup vs. 300% speed up with the same effort) to bite the bullet and write some C.

2 of 9
31

If you check the info on python.org, you can see this summary:

Version Time (seconds)
Basic loop 3.47
Eliminate dots 2.45
Local variable & no dots 1.79
Using map function 0.54

But you really should read the above article in details to understand the cause of the performance difference.

I also strongly suggest you should time your code by using timeit. At the end of the day, there can be a situation where, for example, you may need to break out of for loop when a condition is met. It could potentially be faster than finding out the result by calling map.