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
function, you can create a concise and inline way to specify the filtering condition. Python filter()
with the lambda expression is the most used way to filter the elements from the iterable like set, list, tuple, and dictionary.
Let’s first quickly see the filter()
function and lambda expression usage separately in Python and then we will learn how to use them together with an example.
Related: Python filter() with List
1. Quick Examples of filter() with Lambda
If you are in a hurry, below are some quick examples of using filter() and lambda together.
# Quick examples of filter() with lambda
# Example 1: Lambda example
# With single argument
square = lambda x: x * x
print(square(4))
# Example 2: Using filter()
# With user defined function
numbers = [1,2,3,4,5,6,7,8]
def even(i):
return (i%2 == 0)
print(list(filter(even, numbers)))
# Example 3: Lambda with filter()
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
# Example 4: Filter list from another list
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
filter_list = [3,5,7,8]
result = list(filter(lambda x: x not in filter_list, numbers))
2. Usage of Lambda Expression
A lambda is a small anonymous function, an anonymous meaning function without a name. It can take a single argument, executes the expression, and returns a result.
In the below example, the lambda function lambda x: x * x
takes a single argument x
and returns the square of that argument (x*x
). When you call the lambda function with the argument 4
, it calculates and returns the square of 4, which is 16
.
# Syntax of lambda
lambda arguments: expression
# Lambda example with single argument
square = lambda x: x * x
print(square(4))
# Output:
# 16
3. Usage of filter()
The filter()
is a built-in function that is used to filter the elements from the iterable object like list, set, tuple, etc. This function will directly filter the elements in an iterable by taking the condition as a function. Here is an example.
# Syntax of filter()
filter(function, iterable)
# Usage of filter()
# Create numbers list
numbers = [1,2,3,4,5,6,7,8]
print("Numbers:",numbers)
# Function that return True for even.
def even(i):
return (i%2 == 0)
# Using filter() with user defined function
print("Even Numbers: ",list(filter(even, numbers)))
Yields below output.
4. Python Lambda with Filter() Example
Let’s see the same above example by using the Python filter()
with lambda, By using these two together you can write the above example(finding even numbers) in a single line.
In the below example, we are filtering out the odd numbers from the list and converting the result into a list using the list()
function. Here, the lambda function takes one argument x
and returns True
if x
is even, False
otherwise.
# Lambda with filter()
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(even_numbers)
# Output:
# [2, 4, 6, 8]
5. Get List Values not Present in Another List
Let’s see another example of filtering list values using the python filter with lambda, here, I have taken two lists named numbers
and filter_list
, Lambda function is executed for each element from the numbers and in lambda expression we check if this element is not in filter_list, if it is not it returns True otherwise False. Finally, it returns all values from numbers
list that are not present in filter_list
.
# Filter list from another list
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
filter_list = [3,5,7,8]
result = list(filter(lambda x: x not in filter_list, numbers))
print(result)
# Output:
# [1, 2, 4, 6]
Frequently Asked Questions on filter() with Lambda in Python
The filter()
function in Python is used to filter elements from an iterable based on a specified condition. It takes a function and an iterable as arguments and returns an iterator containing only the elements for which the function returns True
.
filter()
works with lambda
by providing a concise and inline way to specify the filtering condition. The lambda
function is used to define the condition, and filter()
applies this condition to each element in the iterable.
The filter()
function in Python can be used with various iterables besides lists. It can be applied to any iterable object, such as tuples, sets, and strings. The key requirement is that the iterable must contain elements that can be evaluated as True
or False
based on the filtering condition.
lambda
function is an anonymous function defined using the lambda
keyword. It can take any number of arguments but can only have one expression. Lambda functions are often used for short, one-time operations.
There are alternative ways to achieve filtering in Python, and the choice often depends on personal preference, readability, and the specific requirements of your code
While filter()
is typically used for element-wise filtering, if you need to filter based on the index, other techniques like list comprehensions or enumerate()
can be more suitable.
Conclusion
By using the python filter()
and lambda together you can filter the iterable elements in a single line. Here, you have learned the usage of lambda and filter()
function separately with an example and learned how to rewrite the same example by using these two together.
Happy Learning!!
Related Articles
- Sort using Lambda in Python
- Python Lambda using if else
- Python Lambda with Multiple Arguments
- Python map() with Lambda Function
- Using filter() with Lambda in Python
- Python map() with Multiple Arguments
- Relative Imports in Python 3
- How to Sort List of Strings in Python
- How to Sort Dictionary by Value in Python
- Python Sort Dictionary by Key