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

🌐
GeeksforGeeks
geeksforgeeks.org › python › lambda-filter-python-examples
Lambda and filter in Python Examples - GeeksforGeeks
April 8, 2025 - It can take any number of arguments but has only one expression. filter() function: A built-in function that filters elements from an iterable based on a condition (function) that returns True or False.
Discussions

How do I use filter and lambda in a situation where lambda takes two arguments?
Why does your lambda take two parameters? Rewrite it so it only takes one. In any event, I think writing a list comprehension would be cleaner than using filter(): filtered_divs = [div for div in divs if f(div)] More on reddit.com
🌐 r/learnpython
7
1
October 3, 2020
How often do you guys use Lambda?
I kinda view them as a shorthand, not required but sometimes nice to have. Stuff like sorting list of lists by index makes sense to use lambdas, but you can fall into the trap of having unreadable oneliners, so some self-restraint is required. If you meant the AWS Lambda the service, I like it IF you have a good way of managing dependancies or are comfortable with using the standard lib for everything. More on reddit.com
🌐 r/Python
218
225
January 18, 2023
Multiple conditions in python filter function
No, why would you expect your output to include 2 when your condition explicitly says x!=2? The other condition excludes any even number, since an odd number modulus 2 returns 1, and 1 converts to True. 0 converts to False. So, you're explicitly filtering out any number evenly divisible by 2. More on reddit.com
🌐 r/learnprogramming
4
1
January 7, 2019
Applying multiple filters to a list.
[x for x in L if f1(x) and f2(x)]? More on reddit.com
🌐 r/Python
9
9
September 1, 2017
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › using filter() with lambda in python
Using filter() with Lambda in Python - Spark By {Examples}
May 31, 2024 - In Python, the filter() function is used to filter elements of an iterable (e.g., a list) based on a certain condition. When combined with the lambda
🌐
Finxter
blog.finxter.com › how-to-filter-in-python-using-lambda-functions
How to Filter in Python using Lambda Functions? – Be on the Right Side of Change
The second argument is the iterable to be filtered—the lambda function checks for each element in the iterable whether the element pass the filter or not. The filter() function returns an iterator with the elements that pass the filtering condition. lst = [1, 2, 3, 4, 5] # Filter all elements ...
🌐
CodeFatherTech
codefather.tech › home › blog › python: filter a list with a lambda function
Python: Filter a List With a Lambda Function - CodeFatherTech
December 8, 2024 - By using the filter() function, given a list, you can create a new list that contains only the elements in the original list that match a filtering condition. A lambda function allows you to define this condition. For example, given a list of numbers, let’s see how to create a list that only ...
🌐
Medium
medium.com › @pivajr › pythonic-tips-using-filter-and-lambda-functions-for-efficient-filtering-61c6e0f81630
Pythonic Tips: Using filter and Lambda Functions for Efficient Filtering | by Dilermando Piva Junior | Medium
March 2, 2025 - The filter() function can be used for a variety of purposes beyond comparing numerical values. It can be used to filter words in a list, remove null values, or even filter objects based on attributes.
🌐
freeCodeCamp
freecodecamp.org › news › lambda-functions-in-python
Lambda Functions in Python – How to Use Lambdas with Map, Filter, and Reduce
June 14, 2024 - Wrapping the map call with list ensures the lambda function is executed for each pair. As a result, the code prints the multiplication results for each pair in the list, showing outputs like "2 3 = 6", "4 5 = 20", and "6 * 7 = 42".
Find elsewhere
🌐
Note.nkmk.me
note.nkmk.me › home › python
Filter (Extract/Remove) Items of a List with in Python: filter() | note.nkmk.me
May 15, 2023 - Extract, replace, convert elements of a list in Python · The first argument of filter() is a callable object such as a function to be applied, and the second argument is an iterable object such as a list.
🌐
Delft Stack
delftstack.com › home › howto › python › filter lambda python
The filter() Method and Lambda Functions in Python | Delft Stack
February 23, 2025 - These functions can be used along with the filter method. The lambda functions have the following syntax. ... Parameters should be comma-separated, and the expression should return a boolean result (True or False). Let us understand how to use the two together using a simple example. Refer to the following Python code for the same. array = [11, 23, 13, 4, 15, 66, 7, 8, 99, 10] new_array = list(filter(lambda x: x <= 20, array)) print("Old Array:", array) print("New Array:", new_array)
🌐
w3resource
w3resource.com › python-exercises › lambda › python-lambda-exercise-5.php
Python: Filter a list of integers using Lambda - w3resource
July 12, 2025 - Similarly, it uses the "filter()" function with a lambda function to filter odd numbers from the 'nums' list. The lambda function checks if the number is odd (x % 2 != 0) and creates a new list called 'odd_nums' containing only the odd numbers.
🌐
Mimo
mimo.org › glossary › python › filter
Python filter(): Syntax, Usage, and Examples
You can also use a lambda function instead of defining a named one: ... You should reach for filter() when you want to streamline conditional selection. It's especially useful for: Keeping only valid data entries from a list or array ... Instead of managing temporary lists and for loops manually, ...
🌐
iO Flood
ioflood.com › blog › python-filter-list
Using Python to Filter a List: Targeted Guide
June 18, 2024 - Here’s an example of how you ... 8, 9, 10] # We use a lambda function to check if a number is even even_numbers = filter(lambda x: x % 2 == 0, numbers) # We convert the filter object to a list and print it print(list(eve...
🌐
Medium
codefather-tech.medium.com › python-filter-a-list-with-a-lambda-function-codefather-407d1195ba00
Python: Filter a List With a Lambda Function — CODEFATHER | by Claudio Sabato | Medium
December 8, 2023 - A lambda function allows you to define this condition. For example, given a list of numbers, let’s see how to create a list that only includes the negative ones. ... Open the Python shell and execute this lambda passing a couple of numbers to it.
🌐
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 - >>> map(lambda x: x+1,my_list) [1, 2, 3, 4, 5, 6, 7] filter() function does the same but the difference being that it filters out those elements which satisfy a specific condition.
🌐
DEV Community
dev.to › bybydev › top-3-methods-to-filter-a-list-in-python-19fe
Top 3 methods to filter a list in Python - DEV Community
May 3, 2024 - # Given list of numbers numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # Use filter() with a lambda function to filter out even numbers even_numbers = list(filter(lambda num: num % 2 == 0, numbers)) print(even_numbers) # [2, 4, 6, 8, 10] For those who are new to Python or programming in general, a for loop with if conditions can be more readable and understandable.
🌐
Cisco
ipcisco.com › home › python lambda
Python Lambda | Lambda & Filter Function | Lambda & Map ⋆ IpCisco
January 10, 2022 - Here, filter function takes two arguments. The first one is lambda function and the second one is list.
🌐
GUVI
guvi.in › hub › python › filtering-a-list-using-the-filter()-method-in-python
Filtering a list using the filter() method in Python
With the 'filter()' function, you ... condition. By specifying a filtering function or lambda expression, the filter() function constructs a new iterable containing only the elements that satisfy the condition....
🌐
IncludeHelp
includehelp.com › python › lambda-and-filter-with-example.aspx
Python filter() with Lambda Function
List of Integers: The original list, fibo, contains the first few numbers of the Fibonacci sequence. filter() with lambda: The filter() function is used to iterate through the fibo list and apply the lambda function to each element.
🌐
Finxter
blog.finxter.com › home › learn python blog › python filter(lambda multiple conditions)
Python filter(lambda multiple conditions) - Be on the Right Side of Change
February 5, 2024 - The lambda function takes an accumulator acc and element x, appending x when it meets the conditions. In summary, there are several methods to filter a list in Python by multiple conditions.
🌐
FavTutor
favtutor.com › blogs › filter-list-python
Python filter() Function: 5 Best Methods to Filter a List
November 11, 2023 - Or you can use the list() function to convert the iterator to a list. ... Filter() function is mostly used for lambda functions to separate lists, tuples, or sets for which a function returns True.