Recently, a member of the Python user group asked about replacing the for loop with lambda in Python. Python offers the ability to use lambda functions, which can sometimes replace traditional for loops. In this tutorial, I’ll guide you through how to replace for loops with lambda functions in Python with some real examples.
To replace a for loop with a lambda function in Python, you can use the map function. For example, if you have a list of numbers and want to square each number, you can use map(lambda x: x ** 2, numbers) instead of a traditional for loop. This approach applies the lambda function lambda x: x ** 2 to each element in the list, resulting in a new list of squared numbers.
What is a Lambda Function?
A lambda function in Python is a small anonymous function defined with the keyword lambda. Unlike regular functions defined using the def keyword, lambda functions can have any number of arguments but only one expression. They are often used for short, throwaway functions or in situations where using a full function would be overkill.
Syntax:
Here is the syntax.
lambda arguments: expression
Why Use Lambda Functions in Python?
Lambda functions are useful for creating small, one-off functions on the fly. They can make your code cleaner and more readable when used appropriately. By replacing for loops with lambda functions, you can write more functional-style code, which is often easier to understand and maintain.
Check out How to Start a Python For Loop at Index 1
Replace For Loops with Lambda Functions in Python
There are various methods to replace for loop with lambda functions in Python. Let me show you different methods with examples.
Basic Example
Let’s start with a simple example of a for loop and how to replace it with a Python lambda function.
Traditional For Loop:
Here is how the traditional for loop in Python looks like:
numbers = [1, 2, 3, 4, 5]
squared_numbers = []
for num in numbers:
squared_numbers.append(num ** 2)
print(squared_numbers)
Using Lambda and map:
Here is the lambda function replacement.
numbers = [1, 2, 3, 4, 5]
squared_numbers = list(map(lambda x: x ** 2, numbers))
print(squared_numbers)
In this example, the map function applies the lambda function lambda x: x ** 2 to each element in the list numbers, effectively replacing the for loop.
Here is the exact output you can see in the screenshot below:

Using Lambda with filter
Another common use case is filtering elements from a list.
Traditional For Loop:
Below is the for loop Python code.
numbers = [1, 2, 3, 4, 5]
even_numbers = []
for num in numbers:
if num % 2 == 0:
even_numbers.append(num)
print(even_numbers)
Using Lambda and filter:
numbers = [1, 2, 3, 4, 5]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(even_numbers)
Here, the filter function uses the lambda function lambda x: x % 2 == 0 to filter out only the even numbers from the list.
Here is the output in the screenshot below:

Check out How to Skip an Iteration in a For Loop in Python?
Using Lambda with reduce
The reduce function from the functools module can also be used to replace for loops, especially when you need to reduce a list to a single value.
Traditional For Loop:
from functools import reduce
numbers = [1, 2, 3, 4, 5]
product = 1
for num in numbers:
product *= num
print(product)
Using Lambda and reduce:
from functools import reduce
numbers = [1, 2, 3, 4, 5]
product = reduce(lambda x, y: x * y, numbers)
print(product)
In this example, reduce applies the lambda function lambda x, y: x * y cumulatively to the items of the list, reducing it to a single product.
Combine Map, Filter, and Reduce
You can also combine map, filter, and reduce for more complex operations.
Example:
from functools import reduce
numbers = [1, 2, 3, 4, 5]
# Filter out even numbers, square them, and then find the product
result = reduce(lambda x, y: x * y, map(lambda x: x ** 2, filter(lambda x: x % 2 == 0, numbers)))
print(result)
In this example, filter is used to get even numbers, map is used to square them, and reduce is used to find the product of the squared numbers.
You can see the output in the screenshot below:

Conclusion
In this tutorial, I have explained how to replace for loops with lambda functions in Python by using different methods, such as using functions like map, filter, and reduce. Whether you filter, map, or reduce data, lambda functions are powerful for writing elegant Python code.
Feel free to experiment with these techniques in your own projects to see how they can simplify your code. If you have any questions or need further clarification, please leave a comment below, and I will try to reply ASAP.
You may also like:

I’m Michelle Gallagher, a Senior Python Developer at Lumenalta based in New York, United States. I have over nine years of experience in the field of Python development, machine learning, and artificial intelligence. My expertise lies in Python and its extensive ecosystem of libraries and frameworks. Throughout my career, I’ve had the pleasure of working on a variety of projects that have leveraged my skills in Python and machine learning. Read more…