Exploring Filter and Lambda in Python

In Python, functions are one of the building blocks of the language. We use them to define reusable pieces of code to call them multiple times whenever needed with customisable inputs. Two of the most useful types of functions are the filter and lambda in Python.

What is the filter Function?

A Python built-in function taking a function and a sequence as arguments which in turn returns a new sequence consisting the items from the original sequence for which function returns True. The general syntax of the filter function is as follows:

filter(function, sequence)

Here, function is the function that will be applied to each element in sequence, and sequence is the iterable that will be filtered.

Let’s look at an example where we want to filter out all the even numbers from a given list of numbers:

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
result = filter(lambda x: x % 2 == 0, numbers)
print(list(result))

Output:

In this example, we use a lambda function to define the filtering criterion, where the lambda function takes x and returns True if x(numbers in the sequence) is even and False otherwise. We pass this lambda function and the numbers list to the filter function, which returns a new list of the even numbers from the original list after applying the appropriate condition.

What is a Lambda Function?

A lambda function is a small anonymous function that allows a function to accept an arbitrary number of arguments, while restricting the body of the function to a single expression. We can use them when we need a small, one-time-use function not requiring a formal definition. The general syntax of a lambda function is as follows:

lambda arguments: expression

Let’s define a function taking two arguments, returning their sum. We can define this function using a lambda function as follows:

sum = lambda x, y: x + y
print(sum(2, 3))
Output of the lambda expression
Output of the above code

In this example, we define a lambda function sum that takes two arguments x and y and returns their sum. We then call this function with the arguments 2 and 3, which returns the value 5.

Lambda filter array python

One of the most powerful features of Python is the ability to combine functions to create more complex operations. In the case of the filter function, we can use lambda functions to define the filtering criterion. Let’s look at another example:

names = ['Alice', 'Bob', 'Charlie', 'Dave']
result = filter(lambda x: len(x) <= 4, names)
print(list(result))
Output of filter and lambda expression
Output

In this example, we use a lambda function to define the filtering criterion. The lambda function takes one argument x, which represents a name in the names list. The lambda function returns True if the length of x is less than or equal to 4 and False otherwise. We pass this lambda function and the names list to the filter function, which returns a new list containing only the names that have a length of 4 or less.

Lambda filter multiple conditions python

We can use the filter() function with a lambda function that contains multiple conditions in Python to filter a list or other iterable based on those conditions.

Here is an example that demonstrates how to use filter() with a lambda function containing multiple conditions:

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Filter the list to include only even numbers greater than 5
filtered_numbers = list(filter(lambda x: x % 2 == 0 and x > 5, numbers))

print(filtered_numbers)

Python filter lambda not in list

You can use a lambda function with the filter() function in Python to filter elements of an iterable that are not present in a given list.

Here is an example that demonstrates how to use filter() with a lambda function to filter out elements that are not present in a list:

my_list = ['apple', 'banana', 'cherry', 'date', 'elderberry', 'fig']
exclude_list = ['banana', 'date', 'fig']

# Filter the list to include only elements not present in exclude_list
filtered_list = list(filter(lambda x: x not in exclude_list, my_list))

print(filtered_list)

In this example, we have a list of strings my_list that contains some fruits. We also have a list of strings exclude_list that contains some fruits we want to exclude from my_list. We define a lambda function that checks if an element is not in exclude_list using the not in operator. We pass this lambda function to the filter() function, along with my_list. We then convert the result of filter() to a list using the list() function and store the filtered list in the filtered_list variable. Finally, we print the filtered list, which contains all the elements of my_list that are not in exclude_list.

Lambda filter map reduce python

In Python, we can use the lambda, filter(), map(), and reduce() functions together as a powerful toolset for data processing and manipulation.

Here’s an example that demonstrates how to use these functions together:

from functools import reduce

my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Filter the list to include only even numbers
filtered_list = list(filter(lambda x: x % 2 == 0, my_list))

# Map the filtered list to a new list of squares
squared_list = list(map(lambda x: x ** 2, filtered_list))

# Reduce the squared list to the sum of its elements
sum_of_squares = reduce(lambda x, y: x + y, squared_list)

print(sum_of_squares)

Here, we start with a list of integers my_list. We use filter() and a lambda function to filter out all the odd numbers and keep only the even numbers, and store the result in filtered_list. We then use map() and a lambda function to square each element of filtered_list, and store the result in squared_list. Finally, we use reduce() and a lambda function to add up all the elements of squared_list to get the sum of their squares, and store the result in sum_of_squares.

FAQs

Can I use a lambda function as a standalone function?

Yes, you can use a lambda function as a standalone function in Python.

What is filter() in Python?

filter() in Python is a built-in-function allowing us to filter elements from an iterable (such as a list, tuple, or string), subject to given conditions.

What is map() in Python?

map() function in Python allows us to apply a specified function to each element in an iterable (such as a list, tuple, or set) and returns an iterator containing the results.

What is reduce() in Python?

reduce() is a function in the functools module of Python that allows you to reduce an iterable to a single value by repeatedly applying a specified function to the elements of the iterable.

How to use map with lambda Python?

map(lambda arguments: expression, iterable) is how we can use map with lambda in Python.

Conclusion

Today, we learnt about the filter function and lambda functions in Python. We have seen how to use the filter function to filter a sequence based on a given criterion and how to use lambda functions to define the filtering criterion. We have also seen how to combine filter and lambda functions to create more complex operations.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments