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: expressionFor example:
square = lambda x: x * x
print(square(5)) # Output: 25Now, 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.

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.

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.

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.

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:
- Python For Loop with Index
- Use Python While with Assignment
- Python While Multiple Conditions
- Add Elements to a List in Python using a For Loop

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.