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 › filter-in-python
filter() in python - GeeksforGeeks
DSA Python · Data Science · NumPy · Pandas · Practice · Django · Flask · Last Updated : 18 Mar, 2026 · filter() function is used to extract elements from an iterable (like a list, tuple or set) that satisfy a given condition.
Published: March 18, 2026
🌐
W3Schools
w3schools.com › python › ref_func_filter.asp
Python filter() Function
Remove List Duplicates Reverse a String Add Two Numbers · Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Training · ❮ Built-in Functions · Filter the array, and return a new array with only the values equal to or above 18: ages = [5, 12, 17, 18, 24, 32] def myFunc(x): if x < 18: return False else: return True adults = filter(myFunc, ages) for x in adults: print(x) Try it Yourself » ·
🌐
Mimo
mimo.org › glossary › python › filter
Python filter(): Syntax, Usage, and Examples
It's particularly useful when working with various data structures in Python. The built-in filter() function creates an iterator from an iterable (like a list) by keeping only the items for which a given function returns True.
🌐
KDnuggets
kdnuggets.com › 2022 › 11 › 5-ways-filtering-python-lists.html
5 Ways of Filtering Python Lists - KDnuggets
November 14, 2022 - Filter the list elements using for loop, list comprehension, regex, filter(), and lambda functions.
🌐
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 - In Python, you can use filter() to filter (extract/remove) items of a list, tuple, or other iterable that satisfy the conditions. Built-in Functions - filter() — Python 3.11.3 documentation Basic usa ...
Find elsewhere
🌐
FavTutor
favtutor.com › blogs › filter-list-python
Python filter() Function: 5 Best Methods to Filter a List
November 11, 2023 - You can also check how to print a list in python. The filter() method is a built-in python function that filters the given set of iterable with the help of a function that tests each element in the sequence to be True or False.
🌐
DataCamp
datacamp.com › tutorial › python-filter
Python filter(): Keep What You Need | DataCamp
June 27, 2025 - As its name makes it clear, filter() ... use it, and how it compares to other filtering tools. Have a read! filter() lets you pull out elements from a list, tuple, or any iterable, but only if they pass your test....
🌐
YouTube
youtube.com › watch
How Do You Filter A List in Python? 🐍 - YouTube
🧠 Filtering a List in Python - Mastering List Filtering TechniquesLearn how to filter lists in Python using both traditional functions and powerful lambda e...
Published: September 2, 2025
🌐
University of Pittsburgh
sites.pitt.edu › ~naraehan › python3 › list_comprehension.html
Python 3 Notes: List Comprehension
Python 3 Notes [ HOME | LING 1330/2330 ] List Comprehension << Previous Note Next Note >> On this page: list comprehension [f(x) for x in li if ...]. Filtering Items In a List Suppose we have a list. Often, we want to gather only the items that meet certain criteria.
🌐
Python
docs.python.org › 3 › builtins › functions.html
Built-in Functions — Python 3.14.7 documentation
Note that filter(function, iterable) is equivalent to the generator expression (item for item in iterable if function(item)) if function is not None and (item for item in iterable if item) if function is None.
🌐
IONOS
ionos.com › digital guide › websites › web development › python filter function
What is Python’s filter function and how to use it
May 26, 2025 - The filter() function in Python lets you apply a condition to an iterable and select the elements that satisfy the condition. What follows is an iterator that contains the filtered results.
🌐
Medium
medium.com › @pythonchallengers › python-challenge-filter-integers-from-a-list-82a4c43efc8d
Python Challenge: Filter integers from a list | by Python Challengers | Medium
December 6, 2023 - The function returns the new list containing only the integer values. ... The function uses the filter() function along with a lambda function to iterate through each element x in the input list lst.
🌐
CodeSignal
codesignal.com › learn › courses › projection-filtering-and-aggregation-of-data-streams › lessons › implementing-data-filtering-in-python
Implementing Data Filtering in Python
Previous LessonNext Lesson: Data Projection Techniques in Python ... class DataFilter: def filter_with_filter_function(self, data_stream): return list(filter(lambda item: item < 10, data_stream))
🌐
Python Tutorial
pythontutorial.net › home › python basics › how to filter list elements in python
How to Filter List Elements in Python By Practical Examples
October 8, 2020 - Summary: in this tutorial, you’ll learn how to filter list elements by using the built-in Python filter() function.
🌐
Real Python
realpython.com › ref › builtin-functions › filter
filter() | Python’s Built-in Functions – Real Python
The built-in filter() function in Python is used to process an iterable and extract items that satisfy a given condition, as determined by a filtering function. filter() returns an iterator that yields only the elements for which the filtering ...
🌐
Reddit
reddit.com › r/learnpython › best/most efficient filtering for lists
r/learnpython on Reddit: Best/Most Efficient Filtering For Lists
October 14, 2025 -

Given a list like all = [1, 2, 3, 4, 5, 6, 7, 8, 9] Which of the following methods is best to return odd values?

#Method 1
odds = [val for val in all if val % 2 != 0]

#Method 2
def is_odd(val):
    return val % 2 != 0


odds = list(filter(is_odd, all))

#Method 3
odds = list(filter(lambda x: x % 2 != 0, all))

#Method 4
odds = []

for val in all:
    if val % 2 != 0:
        odds.append(val)
🌐
Reddit
reddit.com › r/learnpython › python filter a list of class objects by class method using filter built-in function problem
r/learnpython on Reddit: Python filter a list of class objects by class method using Filter built-in function problem
January 21, 2022 -

Like ok... the title said it all. For example : I have a list filled by class name Food. The class uniqueness are defined by name and price attribute. And I create a method to check if the Food is free. the question is how do I use the method to filter the list of Food class using the free checking method of food class?

@dataclass
class Food:
    name : str
    price : int

    def isfree(self):
        return self.price == 0

food_list = [Food(name = "curry", price = 5), Food(name = "sushi", price = 0)]
filtered_list = filter(Food.isfree, food_list) # got error TypeError: isfree() takes 0 positional arguments but 2 were given
🌐
Reddit
reddit.com › r/learnpython › filtering list of objects on multiple conditions
r/learnpython on Reddit: Filtering list of objects on multiple conditions
July 27, 2023 - I had a thought that this might be easier to by creating something like a PlayerFilter class which can be instantiated and then have filters added to a dict attribute or similar, and then can be applied to a list of players and return the filtered list. This seems more Pythonic to me but also ...
🌐
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.