Home »
Python
Iterating with Python Lambda
Last Updated : April 24, 2025
In Python programming language, an anonymous function i.e., a function without a name and lambda keyword is known as the Lambda function. Lambda function is used as a function object. Here, we will learn how to iterate the elements with lambda function?
Iterating with Lambda using For Loop
You can iterate Python lambda expressions using a basic for loop. This allows you to apply a lambda function on each element of an iterable, such as a list. The lambda function is executed for each element, and the result can be stored in another list.
Example
In the below example, we have a list of integers. We will iterate over its elements and find the cubes of all elements using a lambda function.
# Python program to demonstrate the example to
# iterate with lambda
# list of integers
numbers = [10, 15, 20, 25, 30]
# list to store cubes
cubes = []
# for loop to iterate over list
for x in numbers:
# lambda expression to find cubes
res = lambda x: x**3
# appending to the list (cubes)
cubes.append(res(x))
# print the lists
print("numbers:", numbers)
print("cubes:", cubes)
The output of the above code will be:
numbers: [10, 15, 20, 25, 30]
cubes: [1000, 3375, 8000, 15625, 27000]
Iterating with Lambda using map() and filter()
You can also iterate Python lambda expressions using the combination of the map()
and filter()
functions. The filter()
function allows you to filter elements based on a condition, and the map()
function applies a lambda to each filtered element.
Example
In the below example, we have a list of integers. We will filter out the even elements and then use a lambda function to find the cubes of the filtered elements.
# Python program to demonstrate the example to
# iterate with lambda
# list of integers
numbers = [10, 15, 20, 25, 30]
# finding cubes of even numbers
# and storing them into cubes
cubes = list(map(lambda x: x**3, filter(lambda y: y%2==0, numbers)))
# print the lists
print("numbers:", numbers)
print("cubes:", cubes)
The output of the above code will be:
numbers: [10, 15, 20, 25, 30]
cubes: [1000, 8000, 27000]
Iterating with Lambda using List Comprehension
Another effective way to iterate Python lambda expressions is using list comprehension. This approach combines both the iteration and conditional filtering into a single line of code. List comprehension is especially useful when you want to perform operations on elements while keeping the code compact.
Example
In the below example, we use list comprehension to find the cubes of all even elements in the list.
# Python program to demonstrate the example to
# iterate with lambda using list comprehension
# list of integers
numbers = [10, 15, 20, 25, 30]
# list comprehension to find cubes of even numbers
cubes = [x**3 for x in numbers if x % 2 == 0]
# print the lists
print("numbers:", numbers)
print("cubes:", cubes)
The output of the above code will be:
numbers: [10, 15, 20, 25, 30]
cubes: [1000, 8000, 27000]
Iterating with Python Lambda Exercise
Select the correct option to complete each statement about using lambda functions for iteration in Python.
- The ___ function can be used with a lambda to apply a function to every item in an iterable.
- The ___ function, when used with lambda, returns only the elements that satisfy a condition.
- The ___ function, available in functools, is used with lambda to cumulatively apply a function to the items of an iterable.
Advertisement
Advertisement