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
749

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
312

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 - Explanation: lambda x: x % 2 == 0 returns True for even numbers, and filter() keeps only those elements.
๐ŸŒ
Reddit
reddit.com โ€บ r/pythontips โ€บ unleashing the power of lambda functions in python: map, filter, reduce
r/pythontips on Reddit: Unleashing the Power of Lambda Functions in Python: Map, Filter, Reduce
July 22, 2023 -

Hello Pythonistas!

I've been on a Python journey recently, and I've found myself fascinated by the power and flexibility of Lambda functions. These anonymous functions have not only made my code more efficient and concise, but they've also opened up a new way of thinking about data manipulation when used with Python's built-in functions like Map, Filter, and Reduce.

Lambda functions are incredibly versatile. They can take any number of arguments, but can only have one expression. This makes them perfect for small, one-time-use functions that you don't want to give a name.

Here's a simple example of a Lambda function that squares a number:

square = lambda x: x ** 2

print(square(5)) # Output: 25

But the real power of Lambda functions comes when you use them with functions like Map, Filter, and Reduce. For instance, you can use a Lambda function with `map()` to square all numbers in a list:

numbers = [1, 2, 3, 4, 5]

squared = list(map(lambda x: x ** 2, numbers))

print(squared) # Output: [1, 4, 9, 16, 25]

You can also use a Lambda function with `filter()` to get all the even numbers from a list:

numbers = [1, 2, 3, 4, 5]

even = list(filter(lambda x: x % 2 == 0, numbers))

print(even) # Output: [2, 4]

And finally, you can use a Lambda function with `reduce()` to get the product of all numbers in a list:

from functools import reduce

numbers = [1, 2, 3, 4, 5]

product = reduce(lambda x, y: x * y, numbers)

print(product) # Output: 120

Understanding and using Lambda functions, especially in conjunction with Map, Filter, and Reduce, has significantly improved my data manipulation skills in Python. If you haven't explored Lambda functions yet, I highly recommend giving them a try!

Happy coding!

๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ lambda-functions-in-python
Lambda Functions in Python โ€“ How to Use Lambdas with Map, Filter, and Reduce
June 14, 2024 - 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". The filter function filters elements in an iterable based on a specified predicate.
๐ŸŒ
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
๐ŸŒ
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.
๐ŸŒ
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 - This combination of filter with lambda functions allows you to write more concise and readable code, especially when dealing with simple filtering operations. 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...
Find elsewhere
๐ŸŒ
Mimo
mimo.org โ€บ glossary โ€บ python โ€บ filter
Python filter(): Syntax, Usage, and Examples
It Needs a Function: The first argument to filter() must be a function (or a lambda) that takes one item as an input and returns True or False. Keeps Items that are True: The new collection will only contain items for which the function returned True. Does Not Change the Original: filter() ...
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_lambda.asp
Python Lambda
Lambda functions are commonly used with built-in functions like map(), filter(), and sorted().
๐ŸŒ
Medium
medium.com โ€บ towards-data-science โ€บ understanding-the-use-of-lambda-expressions-map-and-filter-in-python-5e03e4b18d09
Pythonโ€™s Lambda Expressions, Map and Filter | by Luciano Strika | TDS Archive | Medium
July 21, 2022 - So to sum up, lambdas can be clunky, but they also add a lot of expression power to your code. Filter and map can be elegant to some, but donโ€™t add a lot to the table in terms of performance (and are seen as less Pythonic than List Comprehensions by some people).
๐ŸŒ
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 - Apply the function to the iterable elements and extract items whose result is determined to be True. For example, use a lambda expression that returns True when the value is an even number (i.e., the remainder of the value divided by 2 is 0).
๐ŸŒ
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
Lambda functions are particularly useful when you need a simple function for a short period of time, without the need to define a named function. They can make your code more concise and readable, especially when used in combination with other ...
๐ŸŒ
Analytics Vidhya
analyticsvidhya.com โ€บ home โ€บ an explanation to pythonโ€™s lambda, map, filter and reduce
An Explanation to Python's Lambda, Map, Filter and Reduce
October 21, 2024 - All the items of an iterable for which the lambda function evaluates to true will get added to the odd. Letโ€™s say you want to compute a sum of the first five integers. You can write something like the following: nums = [1, 2, 3, 4, 5] summ = 0 for num in nums: summ += num summ # Output: 15 ยท The loop iterates through nums and keeps adding all the numbers to summ. Again to make it pythonic, we have a function, i.e.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ lambda-and-filter-in-python-examples
Lambda and filter in Python Examples
August 7, 2019 - inp_list = ['t','u','t','o','r','i','a','l'] result = list(filter(lambda x: x!='t' , inp_list)) print(result) ... Here all elements in the list other than โ€˜tโ€™ are filtered by the condition and list is formed by the help of lambda expression.
๐ŸŒ
Python Course
python-course.eu โ€บ advanced-python โ€บ lambda-filter-reduce-map.php
4. Lambda Operator, filter, reduce and map | Advanced
Enough that Guido van Rossum wrote hardly a year later: "After so many attempts to come up with an alternative for lambda, perhaps we should admit defeat. I've not had the time to follow the most recent rounds, but I propose that we keep lambda, so as to stop wasting everybody's talent and time on an impossible quest." We can see the result: lambda, map() and filter() are still part of core Python.
๐ŸŒ
Amazon Web Services
docs.aws.amazon.com โ€บ aws lambda โ€บ developer guide โ€บ what is aws lambda?
What is AWS Lambda? - AWS Lambda
January 31, 2026 - Lambda runs your code with language-specific runtimes (like Node.js and Python) in execution environments that package your runtime, layers, and extensions.
๐ŸŒ
IncludeHelp
includehelp.com โ€บ python โ€บ lambda-and-filter-with-example.aspx
Python filter() with Lambda Function
April 25, 2025 - The filter() function is used to filter the elements from given iterable collection based on applied function. The lambda function is an anonymous function - that means the function which does not have any name.
๐ŸŒ
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.
๐ŸŒ
DataFlair
data-flair.training โ€บ blogs โ€บ python-program-on-filter-function-with-lambda-function
Python Program on Filter Function with Lambda Function - DataFlair
February 28, 2024 - The filter() function applies the lambda function to each character in mystring. It returns a filter object that ... The filter object is converted to a list and stored in str1. So str1 contains only the vowels from mystring.