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 - lambda function lambda x: (x == "".join(reversed(x))) checks if the word is equal to its reversed version. filter() function keeps only those words that are palindromes.
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

filter() and map() necessary?
Probably for historic reasons, to cater to people used to functional programming. I've seen a ton of Python code and have barely encountered them. Guido van Rossum (creator of Python) actually wanted to remove them from the language because they promote writing unreadable code (which is against the Zen of Python). More on reddit.com
🌐 r/learnpython
112
194
July 7, 2021
What are lambda, map, filter and reduce functions?
lambda is basically just a short-hand way to write out functions (If you want it ELI5) The notation is name = lambda arg1, arg2, argn : return value So if you make a regular function to square something, it'd look like this: def square(x): return x**2 With lambdas, that would be come square = lambda x : x**2 Lambda functions aren't really the pythonic way of doing things, but they are nice when you need one-time use functions. Maps come from (IIRC) mathematics, where a mapping is just a function. You take values in, and map them to something else. In python, it's basically just a way of taking input values, putting them through some function, and getting some output map. Example: Say we have a list nums = [1,2,3] And we want to square these values. Using regular functions, we could do it like this (using the square function above): output = [] for x in nums: output.append(square(x)) A more pythonic way would be to use list comprehensions, which looks like this output = [square(x) for x in nums] However, we also can use maps - in the following fashion output = map(square, nums) The map takes in a function on the left side, and input on the right side. BUT , this outputs a map. If you'd want it on list form, you'd have to cast it as a list output = list(map(square,nums)) Let's now say that you'll only use that square function once - so we pass it as a lambda instead output = list(map(lambda x : x**2, nums)) Pretty neat, right? Filter is what the name says it is: It filters out values So let's say we want the output list, but only values under 9 output = list(filter(lambda x : x < 9, output)) Same structure as with maps - filtering function to the left, input numbers to the right. Reduce is a fancy way of reducing a list. Maybe you want to sum your list some specific way, like squaring all numbers in the list, and summing them together. output = list(reduce(lambda x, y : x - y, output)) This would be the same as output = output[0]-output[1] and then output = (output[0]-output[1]) - output[2] output = ((output[0]-output[1]) - output[2]) - output[3] etc. It's not very pythonic, but it's used practice from other places. When dealing with big data, you usually do this, while using generators and the likes - basically reading line from line, and doing something. More on reddit.com
🌐 r/learnpython
34
10
December 23, 2019
How to end up this lambda function?
It's not your lambda that's broken, but filter; it expects two arguments, a function (or other callable) and some iterable. You only provided the function. starts_with_h = list(filter(lambda string: string.lower().startswith('h'), list_str)) print(starts_with_h) It would be preferable to use a list comprehension, though. starts_with_h = [ string for string in list_str if string.lower().startswith('h') ] More on reddit.com
🌐 r/learnpython
13
1
November 8, 2022
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
🌐
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
🌐
Mimo
mimo.org › glossary › python › filter
Python filter(): Syntax, Usage, and Examples
... values = ["apple", "", "banana", "", "cherry"] cleaned = list(filter(None, values)) print(cleaned) # Output: ['apple', 'banana', 'cherry'] Passing None as the function filters out all falsy values, including "", 0, False, and None. ... ages = [12, 17, 21, 30, 15] adults = list(filter(lambda ...
🌐
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]...
🌐
Cisco
ipcisco.com › home › python lambda
Python Lambda | Lambda & Filter Function | Lambda & Map ⋆ IpCisco
January 10, 2022 - Python Filter function is a function that takes a lambda function as an argument with also another argument. For example if we would like to create a new list from an existing list that includes only even list items, we can use below code:
🌐
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.
Find elsewhere
🌐
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 ...
🌐
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
When used in combination with lambda ... 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:...
🌐
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']
🌐
TutorialsPoint
tutorialspoint.com › lambda-and-filter-in-python-examples
Python - Lambda Expressions
August 7, 2019 - For instance, if we need a small function that will be used only once such as sorting a list, filtering data or performing a calculation. In this cases, writing the full function feels like unnecessary, this is where we use the Lambda Expressions.
🌐
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 - Lambda is a keyword in Python used to define functions, more specifically Anonymous Functions, and such functions are known as Lambda Functions or Lambda Expressions. Here’s a syntax to define a Lambda Function. ... Let us understand this with an example.
🌐
Python Pool
pythonpool.com › home › tutorials › python filter() with lambda: examples, rules, and better alternatives
Python filter() With lambda: Examples, Rules, and Better Alternatives
July 14, 2026 - In Python 3, filter returns an iterator. It is consumed once, so convert it to list when a reusable snapshot is required, or iterate directly when streaming values is preferable. filter(None, iterable) keeps truthy items and removes falsey values such as 0, empty strings, and None. Use an explicit lambda or function when falsey values are valid data that should be retained.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-lambda-anonymous-functions-filter-map-reduce
Python Lambda Functions - GeeksforGeeks
Explanation: lambda function performs ... results. 4. filter(): This function uses a lambda expression to select elements from a list that satisfy a given condition, such as keeping only even numbers....
Published: May 18, 2026
🌐
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 - Knowledge of basic list and string operations in Python. This program starts by creating a list, ‘mylist,’ comprised of numeric elements and a string, ‘mystring.’ It effectively utilizes the ‘filter’ function in combination with a lambda expression to establish ‘mylist1,’ a new list exclusively housing even numbers extracted from the original list.
🌐
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, ...
🌐
IncludeHelp
includehelp.com › python › lambda-and-filter-with-example.aspx
Python filter() with Lambda Function
The below is the syntax of using filter() with a lambda function in Python: ... The following example demonstrates how to filter even numbers from a list using filter() with a lambda function:
🌐
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.
🌐
Coddy.Tech
coddy.tech › learn › courses › python lambda functions › filter
filter – Python Lambda Functions | Coddy
Vowels are the characters - 'a', 'e', 'i', 'o', 'u'. ... iterable = input() # Don't change this line function = ? # Don't change below this line no_vowels = filter(function, iterable) print(''.join(list(no_vowels)))
🌐
DataCamp
campus.datacamp.com › courses › introduction-to-functions-in-python › lambda-functions-and-error-handling
Filter() and lambda functions | Python
# Create a list of strings: fellowship fellowship = ['frodo', 'samwise', 'merry', 'pippin', 'aragorn', 'boromir', 'legolas', 'gimli', 'gandalf'] # Use filter() to apply a lambda function over fellowship: result ____ = filter(____, ____) # Convert result to a list: result_list ____ # Print ...