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.
🌐
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
🌐
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.
🌐
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!

🌐
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...
🌐
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.
🌐
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.
Find elsewhere
🌐
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
In this tutorial, we will explore ... define anonymous functions, while the filter() function allows us to selectively apply these functions to elements of a sequence....
🌐
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() ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-lambda-anonymous-functions-filter-map-reduce
Python Lambda Functions - GeeksforGeeks
Explanation: lambda function performs both addition and multiplication and returns a tuple with both results. filter() function uses a lambda expression to select elements from a list that satisfy a given condition, such as keeping only even numbers.
Published   4 days ago
🌐
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.
🌐
Delft Stack
delftstack.com › home › howto › python › filter lambda python
The filter() Method and Lambda Functions in Python | Delft Stack
February 23, 2025 - Since this is a common task, the Python programming language has an in-built method, filter(). This method needs a function to perform filtration. If you’re using filter() to check for None values, see [Check if a Variable is None in Python](HowTo/Python/check if variable is none python.en.md). Generally, lambda methods are considered for this job since they are straightforward to write.
🌐
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.
🌐
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 - Today’s piece covers using lambda, map, and filter functions in Python. We’ll be covering the basic syntax of each and walking through some examples to familiarize yourself with using them.
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-use-the-python-filter-function
How To Use the Python Filter Function | DigitalOcean
July 24, 2020 - Overall, lambda functions achieve the same result with filter() as when we use a regular function. The necessity to define a regular function grows as the complexity of expressions for filtering our data increases, which is likely to promote better readability in our code. We can pass None as the first argument to filter() to have the returned iterator filter out any value that Python considers “falsy”. Generally, Python considers anything with a length of 0 (such as an empty list or empty string) or numerically equivalent to 0 as false, thus the use of the term “falsy.”
🌐
w3resource
w3resource.com › python-exercises › lambda › python-lambda-exercise-5.php
Python: Filter a list of integers using Lambda - w3resource
July 12, 2025 - Similarly, it uses the "filter()" function with a lambda function to filter odd numbers from the 'nums' list. The lambda function checks if the number is odd (x % 2 != 0) and creates a new list called 'odd_nums' containing only the odd numbers.
🌐
Cisco
ipcisco.com › home › python lambda
Python Lambda | Lambda & Filter Function | Lambda & Map ⋆ IpCisco
January 10, 2022 - Now, let’s learn what are these functions and how can we use them with lambda function. Python Filter function is a function that takes a lambda function as an argument with also another argument.
🌐
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.
Top answer
1 of 4
10

x = ['1', '2', '4', 'c'], so x[1]=='2', which makes the expression (x[0] != "1" and x[1] != "2" and x[2] != "3") be evaluated as False.

When conditions are joined by and, they return True only if all conditions are True, and if they are joined by or, they return True when the first among them is evaluated to be True.

2 of 4
8
['1', '2', '4', 'c']

Fails for condition

x[0] != "1"

as well as

x[1] != "2"

Instead of using or, I believe the more natural and readable way is:

lambda x: (x[0], x[1], x[2]) != ('1','2','3')

Out of curiosity, I compared three methods of, er... comparing, and the results were as expected: slicing lists was the slowest, using tuples was faster, and using boolean operators was the fastest. More precisely, the three approaches compared were

list_slice_compare = lambda x: x[:3] != [1,2,3]

tuple_compare = lambda x: (x[0],x[1],x[2]) != (1,2,3)

bool_op_compare = lambda x: x[0]!= 1 or x[1] != 2 or x[2]!= 3

And the results, respectively:

In [30]: timeit.Timer(setup="import timeit,random; rand_list = [random.randint(1,9) for _ in range(4)]; list_slice_compare = lambda x: x[:3] != [1,2,3]", stmt="list_slice_compare(rand_list)").repeat()
Out[30]: [0.3207617177499742, 0.3230015148823213, 0.31987868894918847]

In [31]: timeit.Timer(setup="import timeit,random; rand_list = [random.randint(1,9) for _ in range(4)]; tuple_compare = lambda x: (x[0],x[1],x[2]) != (1,2,3)", stmt="tuple_compare(rand_list)").repeat()
Out[31]: [0.2399928924012329, 0.23692036176475995, 0.2369164465619633]

In [32]: timeit.Timer(setup="import timeit,random; rand_list = [random.randint(1,9) for _ in range(4)]; bool_op_compare = lambda x: x[0]!= 1 or x[1] != 2 or x[2]!= 3", stmt="bool_op_compare(rand_list)").repeat()
Out[32]: [0.144389363900018, 0.1452672728203197, 0.1431527621755322]