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

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

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
python - When would using the filter function be used instead of a list comprehension? - Stack Overflow
Recently, I have learned a bit about the filter method, an alternative to using list comprehensions. Say I have a list as such: names = ["Bob", "Billy", "Samuel", &quo... More on stackoverflow.com
🌐 stackoverflow.com
python - Difference between filter and list comprehension - Stack Overflow
here, q contains x*x of each element, ... get past filter selector ... Sign up to request clarification or add additional context in comments. ... Therefore, 2 * 2 = 4 won't be included in the list. In short, if you want to get the same behaviour, modify your list comprehension to square ... More on stackoverflow.com
🌐 stackoverflow.com
At what point are loops better than list comprehensions?
Readability is important, just after correctness. That means, in the initial approximation, speed/efficiency is less important. Until much later anyway. I wouldn't try to write that code as a comprehension. If, later, you need faster code you worry about algorithms first, not the low-level stuff. More on reddit.com
🌐 r/learnpython
56
32
September 19, 2023
🌐
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 - But as you increase the size of ... win: For large lists with one million elements, filtering lists with list comprehension is 40% faster than the built-in filter() method....
🌐
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 - With map() and filter() you are locked into being required to ONLY use iterables as the additional arguments after your method pointer. A list comprehension has no such limitations and you can do whatever the hell awesome amazing stuff you want with as much variations as you like in parameters.
🌐
Recology
recology.info › list comprehension vs. filter vs. key lookup
List comprehension vs. filter vs. key lookup | Recology
April 18, 2022 - For the get() lookup, the list comprehension is 26 times slower, and the filter is 39 times slower.
🌐
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.
🌐
Quantifiedcode
docs.quantifiedcode.com › python-anti-patterns › readability › using_map_or_filter_where_list_comprehension_is_possible.html
Using map() or filter() where list comprehension is possible — Python Anti-Patterns documentation
Although a map() or filter() expression may be functionally equivalent to a list comprehension, the list comprehension is generally more concise and easier to read.
Find elsewhere
🌐
O'Reilly
oreilly.com › library › view › python-cookbook › 0596001673 › ch01s11.html
Using List Comprehensions Instead of map and filter - Python Cookbook [Book]
July 19, 2002 - thenewlist = map(lambda x: x+23, filter(lambda x: x>5, theoldlist)) A list comprehension affords far greater clarity, as we can both perform selection with the if clause and use some expression, such as adding 23, on the selected items:
Authors   Alex MartelliDavid Ascher
Published   2002
Pages   608
🌐
Real Python
realpython.com › lessons › filtering-elements-list-comprehensions
Filtering Elements in List Comprehensions (Video) – Real Python
As you can see, this filtered down the previous list of squares so that it only includes the even squares. So remember, this is what we had previously, before the filtering, and that also included odd numbers—and now, those are all gone because we filtered them out with this if part in the list comprehension.
Published   March 1, 2019
🌐
sqlpey
sqlpey.com › python › python-list-comprehension-vs-filter-debate
Python List Comprehension Versus Filter: Performance and Pythonic Style Debate
November 4, 2025 - Function Call Overhead: Using a function (whether def or lambda) within filter() generally incurs a function call overhead for every item iterated, which list comprehensions often avoid or handle differently internally.
🌐
W3docs
w3docs.com › python
List comprehension vs. lambda + filter | W3Docs
List comprehension and the combination of lambda functions and the filter() function in Python are both used to filter a list and return a new list with only the elements that satisfy a certain condition.
🌐
Pdxdev
python.pdxdev.com › interview-questions › what-is-the-difference-between-filter-and-list-comprehension
What is the difference between ‘filter()’ and list comprehension?
If the function returns True, the element is included in the resulting filter object. Think of it as selectively picking out elements based on a predefined condition. List Comprehensions: A compact and elegant way to create new lists based on existing iterables.
🌐
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...
🌐
Real Python
realpython.com › lessons › python-filter-list-comprehension
Replace filter() With a List Comprehension (Video) – Real Python
02:49 This is how you can use list comprehensions instead of the filter() function to filter iterables.
Published   July 4, 2023
🌐
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?
🌐
Devgex
devgex.com › en › article › 00003072
Comparative Analysis of List Comprehension vs. filter+lambda in Python: Performance and Readability - DevGex
November 1, 2025 - 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. From a syntactic perspective, list comprehension better aligns with Python's philosophy of "explicit is better than implicit."
🌐
Gitbooks
hacktec.gitbooks.io › effective-python › content › en › Chapter1 › Item7.html
Item 7: Use List Comprehensions Instead of map and filter · Effective Python
List comprehensions are clearer than the map and filter built-in functions because they don’t require extra lambda expressions.