Lambda in List Comprehension in Python

I was working on a data-cleaning task where I had to transform and filter a list of U.S. city names. I wanted a quick way to apply conditions and transformations without writing long functions.

That’s when lambda with list comprehension came in handy. This combination gave me cleaner, more Pythonic code.

In this tutorial, I’ll show you exactly how I use lambda inside list comprehensions. I’ll cover filtering, mapping, and real-world use cases.

What is a Lambda Function in Python?

A lambda function in Python is a small, anonymous function. It doesn’t need a name and is usually written in one line.

Here’s the basic syntax:

lambda arguments: expression

For example:

square = lambda x: x * x
print(square(5))  # Output: 25

Now, let’s see how we can combine this with list comprehensions.

Method 1 – Use Lambda in List Comprehension for Mapping

The most common use case is transforming a list of values. Suppose I have a list of U.S. city names, and I want to convert them all to uppercase.

Here’s how I do it:

cities = ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"]

# Using lambda in list comprehension
upper_cities = [(lambda x: x.upper())(city) for city in cities]

print(upper_cities)

Output:

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

You can see the output in the screenshot below.

python list comprehension lambda

This way, the lambda function applies the transformation directly inside the comprehension.

Method 2 – Filter Data with Lambda in List Comprehension

Another powerful use case is filtering data. Let’s say I want only the cities that start with the letter “C”.

cities = ["New York", "Los Angeles", "Chicago", "Houston", "Cleveland"]

# Filter cities starting with 'C'
filtered_cities = [city for city in cities if (lambda x: x.startswith("C"))(city)]

print(filtered_cities)

Output:

['Chicago', 'Cleveland']

You can see the output in the screenshot below.

python lambda function list comprehension

Here, the lambda checks the condition, and the list comprehension keeps only the matching cities.

Method 3 – Apply Multiple Conditions

Sometimes, I need more than one condition. For example, filter cities that start with “C” and have more than 7 characters.

cities = ["New York", "Los Angeles", "Chicago", "Cleveland", "Columbus"]

# Cities starting with 'C' and length > 7
special_cities = [
    city for city in cities 
    if (lambda x: x.startswith("C") and len(x) > 7)(city)
]

print(special_cities)

Output:

['Cleveland', 'Columbus']

You can see the output in the screenshot below.

python lambda list

This shows how flexible lambda can be in list comprehensions.

Method 4 – Use Lambda with Numbers

This method demonstrates how to use a lambda function with list comprehension to perform operations on specific elements in a list. In this example, we square only the even numbers from the given list.

Suppose I want to square only the even numbers in a list.

numbers = [10, 15, 20, 25, 30]

# Square only even numbers
squared_evens = [(lambda x: x * x)(num) for num in numbers if num % 2 == 0]

print(squared_evens)

Output:

[100, 400, 900]

You can see the output in the screenshot below.

python lambda list comprehension

Using lambda with conditional list comprehension allows concise and efficient element-wise operations. It’s a clean way to apply transformations only to elements meeting certain conditions.

Method 5 – Real-World Example: Filtering U.S. ZIP Codes

Let’s take a practical U.S.-specific example. Imagine I have a list of ZIP codes, and I want only those that start with “90” (California).

zip_codes = ["10001", "60601", "90001", "94105", "75201"]

# Filter California ZIP codes (starting with '90')
ca_zip_codes = [z for z in zip_codes if (lambda x: x.startswith("90"))(z)]

print(ca_zip_codes)

Output:

['90001']

This is a real-world way I’ve used lambda in list comprehensions for quick filtering.

When Should You Use Lambda in List Comprehension?

From my experience:

  • Use it when you need quick, one-line operations.
  • Great for mapping (transforming values).
  • Useful for filtering with conditions.
  • Avoid it for very complex logic (better to use a named function).

Most of the time, I use lambda with list comprehensions for quick transformations and filters.
It keeps my code short, clean, and Pythonic.

If you’re working with text, numbers, or even real-world data like ZIP codes or city names, this trick will save you time.

I hope you found this tutorial helpful.
Now, go ahead and try using lambda in your own list comprehensions, you’ll see how powerful it is!

You may also read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.