How to use List comprehension using lambda and filter in Python

Welcome to akashITS (Akash IT Solutions), In this article, you will learn about: How to use List comprehension using lambda and filter in Python

How to use List comprehension using lambda and filter in Python

Python’s filter function, lambda functions, and list comprehension are useful tools for writing clear, creative code. Let’s look at few examples of using lambda and the filter function for list comprehension.

List Comprehension:

List comprehensions provide a concise way to create lists. Common applications are to make new lists where each element is the result of some operations applied to each member of another sequence or iterable, or to create a subsequence of those elements that satisfy a certain condition.

It follows the syntax [expression for item in iterable if condition].

Example:

# Cube numbers from 0 to 4 using list comprehension
squares = [x**3 for x in range(5)]
print(squares)
# Output: [0, 1, 8, 27, 64]

Lambda Functions:

The lambda function, although very powerful, usually causes many doubts. Lambda functions are anonymous functions that don’t need to be defined before they are used. They are also called inline functions and help keep your code more organized.

In some situations we do not need to create a function, that is, defining the function. We create a function only at run time—inline and anonymous, after use is discarded.

Example:

# A lambda function to cube a number
square_func = lambda x: x**3
print(square_func(4))
# Output: 64

List Comprehension with Lambda:

You can use a lambda function within a list comprehension to perform operations on each element of an iterable. For instance, squaring even numbers:

Example:

# Squaring even numbers from 0 to 4 using list comprehension and lambda
squares_of_evens = [(lambda x: x**2)(x) for x in range(5) if x % 2 == 0]
print(squares_of_evens)
# Output: [0, 4, 16]

Filter Function:

The filter function is used to construct an iterator from elements of an iterable for which a function returns True. It takes two arguments: the filtering function and the iterable.

Example:

# Filtering even numbers from 0 to 19 using filter and lambda
even_numbers = list(filter(lambda x: x % 2 == 0, range(20)))
print(even_numbers)
# Output: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

List Comprehension with Filter:

Combining list comprehension with the filter function and a lambda expression allows you to create a new list with elements meeting specific conditions.

Example:

# Squaring even numbers from 0 to 19 using list comprehension, filter, and lambda
squares_of_evens_filtered = [x**2 for x in range(20) if x % 2 == 0]
print(squares_of_evens_filtered)
# Output: [0, 4, 16, 36, 64, 100, 144, 196, 256, 324]

List comprehension with a lambda function and the filter function:

We want to create a new list containing the squares of even numbers. we can achieve this using list comprehension with a lambda function and the filter function:
Example:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Squaring even numbers using list comprehension, filter, and lambda
squares_of_even_numbers = [x**2 for x in numbers if x % 2 == 0]
print(squares_of_even_numbers)
# Output: [4, 16, 36, 64, 100]

The list comprehension in this example squares every element from the original list that meets the filter’s lambda function-specified requirement to create a new list.

An expressive and simple method to perform operations on iterable components while maintaining code readability is to combine lambda functions, the filter function, and list comprehension.

Leave a Comment