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
The following are quick examples of using filter() and lambda together.
# Example 1: Lambda with filter() numbers = [1, 2, 3, 4, 5, 6, 7, 8] even_numbers = list(filter(lambda x: x % 2 == 0, numbers)) # Example 2: 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. Here is an example.
# 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 e.t.c. 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))) # Output: # Numbers: [1, 2, 3, 4, 5, 6, 7, 8] # Even Numbers: [2, 4, 6, 8]
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
x is even,
# 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
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 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]
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.
- 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