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 - Before diving into examples, letโ€™s ... in Python: lambda function: A small, anonymous function defined using the lambda keyword. 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. For example, suppose we want to filter even numbers from a list, here's how ...
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

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
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
๐ŸŒ
Cisco
ipcisco.com โ€บ home โ€บ python lambda
Python Lambda | Lambda & Filter Function | Lambda & Map โ‹† IpCisco
January 10, 2022 - In another example, we can control the items of a list if it is higher than a value or not. list1 = [7,12,4,15,3,2,8] list2 = list(filter(lambda x: (x > 5) , list1)) print(list2) ...
๐ŸŒ
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 ... that pass the filtering condition. lst = [1, 2, 3, 4, 5] # Filter all elements <3 my_list = filter(lambda x: x<3, lst) print(list(my_list)) # [1, 2]...
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ python โ€บ filter lambda python
The filter() Method and Lambda Functions in Python | Delft Stack
February 23, 2025 - The above Python code filters all the values from the list of integer, array, that is less than or equal to 20. Each value of the list is passed to the lambda function. If it returns True, the value is added to the result; otherwise, not. Once the result is obtained in an iterator, it is converted to a list using the in-built list() method. Lastly, both the arrays are printed to the console. Following are some examples to understand the usage of the filter() method and the lambda functions together.
๐ŸŒ
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']
Find elsewhere
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ lambda โ€บ python-lambda-exercise-5.php
Python: Filter a list of integers using Lambda - w3resource
July 12, 2025 - It then prints the list of odd numbers ('odd_nums'). ... print((lambda x: (x % 2 and 'Odd number' or 'Even number'))(5)) print((lambda x: (x % 2 and 'Odd number' or 'Even number'))(8)) ... Write a Python program to filter a list of integers, ...
๐ŸŒ
Netalith
netalith.com โ€บ blogs โ€บ tutorial โ€บ how-to-use-the-python-filter-function
Python filter function: Examples, lambda & list tips | Netalith
February 22, 2026 - Because filter() returns an iterator rather than creating a brand-new list immediately, it can be more memory-efficient than creating a full list with a list comprehension when working with large datasets. 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.
๐ŸŒ
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 - 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.
๐ŸŒ
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 ...
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ lambda-functions-in-python
Lambda Functions in Python โ€“ How to Use Lambdas with Map, Filter, and Reduce
June 14, 2024 - We use a lambda function to define a simple condition that checks if an age is 18 or older. The filter function applies this lambda function to each age in the list, filtering out any ages below 18.
๐ŸŒ
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...
๐ŸŒ
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 - for i in filter(lambda x: x % 2 == 0, l): print(i) # -2 # 0 # 2 ... Note that filter() in Python 2 returns a list, which might cause issues when running Python 2 code in Python 3.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ lambda-and-filter-in-python-examples
Python - Lambda Expressions
August 7, 2019 - In the following example, we are going to use the lambda with the filter() function. array = [5,10,15,20] result = list(filter(lambda x: x % 2 == 0, array)) print(result)
๐ŸŒ
EyeHunts
tutorial.eyehunts.com โ€บ home โ€บ python filter list lambda
Python filter list lambda
January 13, 2023 - numbers = [11, 22, 33, 44, 55, 66, 77, 88, 99, 100] print("List of numbers:") print(numbers) print("\nList of even numbers:") evenNumbers = list(filter(lambda x: x%2 == 0, numbers)) print(evenNumbers) print("\nList Odd numbers:") oddNumbers ...
๐ŸŒ
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.
๐ŸŒ
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. It executes the function_object for each element in the sequence and returns a list of the elements modified by the function object. In the above example, map executes the multiply2 function for each element in the list, [1, 2, 3, 4], and returns [2, 4, 6, 8].
๐ŸŒ
Python Course
python-course.eu โ€บ advanced-python โ€บ lambda-filter-reduce-map.php
4. Lambda Operator, filter, reduce and map | Advanced
Lambda functions are mainly used ... filter(), map() and reduce(). The lambda feature was added to Python due to the demand from Lisp programmers. The general syntax of a lambda function is quite simple: ... The argument list consists of a comma separated list of arguments and the expression is an arithmetic expression using these arguments. You can assign the function to a variable to give it a name. The following example of a lambda ...