Lambda in List Comprehension in Python [3 Examples]

Do you want to know how to approach list comprehension with the lambda function? In this Python tutorial, I will explain how to use lambda in list comprehension in Python.

I will also explain the difference between List comprehension and Lambda in Python.

One of the advanced techniques often used in list comprehensions is the integration of lambda functions in Python. So, combining the lambda function with list comprehension in Python will create a list with conditions applied through a function.

Let’s dive deep into how to use the lambda in list comprehension in Python.

List comprehension in Python

List comprehension in Python is a concise way to generate lists. They provide a compact technique to create a list based on conditions with existing iterables like lists, tuples, or strings.

Let’s first understand the basic syntax of list comprehension in Python.

[expression for item in iterable if condition]

The parameters are:

  • expression: It is the operation or transformation applied to each item.
  • item: It is the variable representing each element in the iterable.
  • iterable: It is the sequence (such as a list, tuple, or string) being iterated over.
  • condition: (optional)This filters whether the item should be included in the resulting list.

Here’s a simple example:

squares = [x**2 for x in range(10)]
print(squares)

Output:

[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

The screenshot below presents the output obtained after the code execution in the Pycharm editor.

Lambda in List Comprehension in Python

Lambda functions in Python

Meanwhile, The Lambda functions in Python, known as anonymous functions, are small and inline functions defined without a name. They are used when a function is required for a short period of time and won’t be reused anywhere else.

The syntax of a Python lambda function is simple:

lambda arguments: expression

The parameters are:

  • arguments: It is a comma-separated list of input parameters.
  • expression: It is a single expression that defines the computation performed by the function.

Python lambda with list comprehension

Now, let’s use the lambda function in list comprehension in Python. Lambda functions are typically used when a function is needed only once and can be expressed concisely.

Here’s how we can use a lambda function within a list comprehension in Python:

  • Lambda Functions in List Comprehension Basic
  • Filtering with Lambda Functions
  • Mapping with Lambda Functions

Python lambda in list comprehension basic

In the context of list comprehension, lambda functions can define simple operations or transformations applied to each item in the iterable in Python.

Here’s a straightforward illustration:

fahrenheit_temps = [32, 50, 68, 86, 104]
celsius_temps = [(lambda f: (f - 32) * 5/9)(temp) for temp in fahrenheit_temps]
print(celsius_temps)

Output: Here, we are modifying the elements of the list in Python with the help of the lambda function and creating a new list with the help of list comprehension.

[0.0, 10.0, 20.0, 30.0, 40.0]

Following implementing the code in the Pycharm editor, the screenshot below has been provided.

python lambda function list comprehension

Filtering with lambda in list comprehension in Python

Filtering with lambda functions in Python is a method of selecting elements from iterables based on conditions.

Lambda in list comprehension in Python to filter out elements that will satisfy the condition. The elements of the iterable that match the conditions return True and will be included in the list that the list comprehension will create in Python.

Here’s an example to illustrate:

state_populations = [39461588, 28995881, 2088070, 19530351, 11330102]
even_population_states = [population for population in state_populations if (lambda x: x % 2 == 0)(population)]
print(even_population_states)

Output: Here, we are using the lambda function to filter the state_populations list in Python, only creating the list of elements that are even in number.

[39461588, 2088070, 11330102]

Below, a screenshot is provided, captured after the code was implemented in the Pycharm editor.

python list comprehension lambda

Mapping with lambda list comprehension in Python

Mapping with the lambda functions in Python is used to apply a transformation to each item in an iterable.

Within a list comprehension, the lambda function is applied to each item in the iterable in Python, and the result of the transformation is included in the new list.

For instance:

cities = ["new york", "los angeles", "chicago", "houston", "phoenix"]
uppercase_cities = [(lambda x: x.upper())(city) for city in cities]
print(uppercase_cities)

Output:

['NEW YORK', 'LOS ANGELES', 'CHICAGO', 'HOUSTON', 'PHOENIX']

After executing the code in Pycharm, the output can be seen in the screenshot below.

python lambda list comprehension

Difference between List comprehension and Lambda in Python

Here’s a table summarizing the differences between list comprehension and lambda functions in Python:

FeatureList ComprehensionLambda Functions
PurposeThis creates a list by applying expressions to iterables in Python.This creates small and anonymous functions in Python.
Syntax[expression for item in iterable if condition]lambda arguments: expression
ScopeCreates a new list in Python.Standalone functions
ComplexityCan handle complex transformations and filtering.Typically simpler, limited to one expression
ReadabilityConcise and expressive.Concise but less expressive.
UsageList creation and data transformation.Short-term or one-off function definitions
Python list comprehension vs lambda.

Conclusion

This article explains how to use lambda in list comprehension in Python. I have explained what list comprehension and lambda in Python are in detail, with examples of how we can use list comprehension with lambda for filtering or mapping the elements of an iterable.

I have also explained the difference between list comprehension and lambda function in Python.

By understanding and utilizing this, developers can effectively manipulate iterable items to suit their needs in various real-world scenarios.

You may like the following Python tutorials: