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

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
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
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
Filtering list of objects on multiple conditions
Hi all, interested in your approaches and suggestions to this problem. Let's say I have created a custom Player class that contains many attributesโ€ฆ More on reddit.com
๐ŸŒ r/learnpython
4
1
July 27, 2023
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ lambda-filter-python-examples
Lambda and filter in Python Examples - GeeksforGeeks
April 8, 2025 - Let's find all anagrams of a given string in a list. ... 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)
๐ŸŒ
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 - Python for loop (with range, enumerate, zip, etc.) for i in filter(lambda x: x % 2 == 0, l): print(i) # -2 # 0 # 2
๐ŸŒ
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.
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
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 the different ways of using the filter() function 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 manipulate the data.
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ python-filter-list
Using Python to Filter a List: Targeted Guide
June 18, 2024 - If the filter() function is given an empty list, it will return an empty filter object. Hereโ€™s an example: # Here is an empty list numbers = [] # We try to filter the empty list filtered_numbers = filter(lambda x: x > 5, numbers) # We convert the filter object to a list and print it print(list(filtered_numbers)) # Output: # []
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ python โ€บ filter lambda python
The filter() Method and Lambda Functions in Python | Delft Stack
February 23, 2025 - Old Array: ['hello', 'python', 'world', 'walking', 'sleep', 'shelter', 'food'] New Array: ['hello', 'world', 'sleep'] array = [11, 23, 13, 4, 15, 66, 7, 8, 99, 10] new_array = list(filter(lambda x: 10 <= x <= 20, array)) print("Old Array:", array) print("New Array:", new_array)
๐ŸŒ
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 - Python provides a built-in function that used with a lambda function allows you to filter a list based on specific conditions: the filter() function. Below you can see the syntax of the filter() function that takes two arguments: the first argument is a function and the second argument is an ...
๐ŸŒ
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...
๐ŸŒ
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.
๐ŸŒ
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 can selectively extract elements from an iterable, such as a list, based on a defined filtering condition. By specifying a filtering function or lambda expression, the filter() function constructs a new iterable ...
๐ŸŒ
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 - Python provides a built-in function that used with a lambda function allows you to filter a list based on specific conditions: the filter() function. Below you can see the syntax of the filter() function that takes two arguments: the first argument ...
๐ŸŒ
IncludeHelp
includehelp.com โ€บ python โ€บ lambda-and-filter-with-example.aspx
Python filter() with Lambda Function
Learn how to use Python's filter() function with lambda to extract elements from iterables based on custom conditions. Includes syntax and examples.
๐ŸŒ
Mimo
mimo.org โ€บ glossary โ€บ python โ€บ filter
Python filter(): Syntax, Usage, and Examples
By using it effectively, you can ... paired with functions, lambdas, or tools like map(), it becomes a key part of your Python programming toolbox. It Returns an Iterator: filter() does not return a list directly. It returns an iterator object, which is memory-effic...
๐ŸŒ
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 - Python offers versatile list filtering through list comprehensions, filter() function with lambda expressions, or comprehensible conditional statements for concise data manipulation.
๐ŸŒ
Better Programming
betterprogramming.pub โ€บ lambda-map-and-filter-in-python-4935f248593
Lambda, Map, and Filter in Python | by Rupesh Mishra | Better Programming
March 19, 2023 - More importantly, lambda functions are passed as parameters to functions that expect function object as parameters such as map, reduce, and filter functions. ... map functions expect a function object and any number of iterables, such as list, dictionary, etc.
๐ŸŒ
HowToDoInJava
howtodoinjava.com โ€บ home โ€บ python โ€บ python list filter() with examples
Python List filter() with Examples
April 3, 2024 - # List of strings strings = ["apple", ... print(filtered_strings_list) ... To filter a list of objects, we need to pass the condition on a certain attribute(s) in the object....