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 › lambda-filter-python-examples
Lambda and filter in Python Examples - GeeksforGeeks
April 8, 2025 - ... from collections import Counter s = ["geeks", "geeg", "keegs", "practice", "aa"] ts = "eegsk" # Using filter() with lambda to find anagrams of target_str res = list(filter(lambda x: (Counter(ts) == Counter(x)), my_list)) print(res)
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

Filter list of object with condition in Python - Stack Overflow
Copy list(filter(lambda x: x['text']=='abc', listpost)) ... Save this answer. ... Show activity on this post. A good pythonic solution is already given, but here's another solution closer to what you were trying: More on stackoverflow.com
🌐 stackoverflow.com
Python filter a list of class objects by class method using Filter built-in function problem
The code runs fine here: https://i.imgur.com/kXq6AhX.png altough I must add that filter has been superseded by list comprehensions, I would advise to use filtered_list = [f for f in food_list if f.isfree()] More on reddit.com
🌐 r/learnpython
2
1
January 21, 2022
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
Unleashing the Power of Lambda Functions in Python: Map, Filter, Reduce
Lambda is Nice. Sometimes, I think a simple list comprehension may look a bit more neat though, even = [k for k in numbers if k%2==0] More on reddit.com
🌐 r/pythontips
5
20
July 22, 2023
🌐
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
🌐
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 - String comparison in Python (exact/partial match, etc.) l_s = ['apple', 'orange', 'strawberry'] print(list(filter(lambda x: x.endswith('e'), l_s))) # ['apple', 'orange'] print(list(filter(lambda x: not x.endswith('e'), l_s))) # ['strawberry'] ... Contrary to filter(), a function itertools.filterfalse() is also provided to keep elements that are False. It is described later. The first argument of filter() is a callable object. As in the previous examples, lambda expressions (lambda) are often used, but of course, it is also possible to specify a function defined with def.
🌐
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
To filter a list in Python, you can use the built-in filter() function. The first argument is the filtering condition, defined as a function. This filtering condition function is often dynamically-created using lambda functions.
🌐
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...
🌐
Delft Stack
delftstack.com › home › howto › python › filter lambda python
The filter() Method and Lambda Functions in Python | Delft Stack
February 23, 2025 - Following are some examples to ... together. array = [1, 2, 3, 4, 5, 66, 77, 88, 99, 100] new_array = list(filter(lambda x: x % 2 == 0, array)) print("Old Array:", array) print("New Array:", new_array) ... array = [1, 2, ...
Find elsewhere
🌐
EyeHunts
tutorial.eyehunts.com › home › python filter list of objects
Python filter list of objects - Tutorial - By EyeHunts
January 25, 2023 - class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary def __repr__(self): return self.name alice = Employee('John', 100) bob = Employee('Mike', 70) carl = Employee('Carl', 150) list_of_objects = [alice, bob, carl] filtered_list = list( filter( lambda obj: obj.salary > 80, list_of_objects ) ) print(filtered_list) ... Do comment if you have any doubts or suggestions on this Python filter topic. ... All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
🌐
GUVI
guvi.in › hub › python › filtering-a-list-using-the-filter()-method-in-python
Filtering a list using the filter() method in Python
In this example, we use a lambda function to check if a number is odd by evaluating the expression 'x%2 != 0'. We pass this lambda function as the first argument to the 'filter()' function, and it filters out only the odd numbers from the 'numbers' list. In conclusion, the 'filter()' function in Python serves as a valuable tool for selectively extracting elements from an iterable based on a specific condition.
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-use-the-python-filter-function
How To Use the Python Filter Function | DigitalOcean
July 24, 2020 - In this tutorial, we’ve learned ... in Python. Now you can use filter() with your own function, a lambda function, or with None to filter for items in varying complexities of data structures. Although in this tutorial we printed the results from filter() immediately in list format, it is likely in our programs we would use the returned filter() object and further ...
🌐
datagy
datagy.io › home › python functions › python filter: a complete guide to filtering iterables
Python filter: A Complete Guide to Filtering Iterables • datagy
December 20, 2022 - Because tuples themselves are ordered, we can access a given index and evaluate if it meets a condition. In the example below, we have a list of tuples that contain dates and their respective sale amounts.
🌐
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.
🌐
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 - Example 1: Filtering Short Words from a String List · words = ["Python", "is", "a", "powerful", "language"] filtered_words = list(filter(lambda word: len(word) > 3, words)) print(filtered_words) # Output: ['Python', 'powerful', 'language']
🌐
Quora
quora.com › How-do-we-filter-elements-from-a-list-using-lambdas-or-anonymous-functions-in-Python-3
How do we filter elements from a list using lambdas or anonymous functions in Python 3? - Quora
The lambda function can be used ... how two objects (say, numbers) will be compared to each other. Something like sort ( [1,4,8,0,5,9] , < ) would sort the list in an increasing order, while sort ( [1,4,8,0,5,9] , > ) in a decreasing order. And you can also use lambdas for currying and closures. Which are topics for more detailed answers! ... RelatedCan you print the square of each element in a list by using a filter and lambda function in Python...
🌐
LabEx
labex.io › tutorials › python-how-to-apply-a-lambda-function-with-the-filter-function-in-python-415783
How to apply a lambda function with the filter() function in Python | LabEx
The filter() function in Python is a built-in function that takes a function and an iterable (such as a list, tuple, or string) as arguments, and returns an iterator that contains only the elements from the iterable for which the function returns True. When used in combination with lambda functions, the filter() function becomes a powerful tool for filtering data based on custom criteria. Here's an example of using filter() with a lambda function to filter a list of numbers and keep only the even numbers:
🌐
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 ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › filter-in-python
filter() in python - GeeksforGeeks
def even(n): return n % 2 == 0 a = [1, 2, 3, 4, 5, 6] b = filter(even, a) print(list(b)) # Convert filter object to a list ... Only even numbers are included in output. Example 2: Below example uses a lambda function with filter() to select ...
Published   March 18, 2026
🌐
Programiz
programiz.com › python-programming › methods › built-in › filter
Python filter()
We can also use the Python filter() function with the lambda function. For example, ... # the lambda function returns True for even numbers even_numbers_iterator = filter(lambda x: (x%2 == 0), numbers) # converting to list even_numbers = list(even_numbers_iterator) print(even_numbers) # Output: ...
🌐
Netalith
netalith.com › blogs › tutorial › how-to-use-the-python-filter-function
Python filter function: Examples, lambda & list tips | Netalith
February 22, 2026 - Below you’ll find several practical python filter examples showing how to use filter() in python for common tasks, including with lambda, with None, and with more complex structures like lists of dictionaries. ... The result is a filter object — an iterator.