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 OverflowIt 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.
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 ;-)
Diffenence between List Comprehension and Map/Filter?
Diffenence between List Comprehension and Map/Filter?
At what point are loops better than list comprehensions?
Remove numbers from list with list comprehension
Videos
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.