The Amazing Fact Why Can’t We use Loops in Python Lambda

Python lambda doesn’t support loops. The syntax of it says it supports arguments and expression. Below are the examples that describe the fact that why we can’t use Loops.

Amazing Fact Why Can't We use For in Python Lambda
Python lambda does not support loops

Here’s syntax for lambda function

Let us check the syntax, the first part of the syntax is an argument, and the second part is expression.

lambda arguments: expression

The keyword lambda is a must to work this function. It follows arguments and expression.

Simple lambda function

Below function receives two arguments. The first one is ‘3’ and the second one is ‘7’.

def mul1(a1):
    return lambda b1:b1*a1
myresult = mul1(3)


The output is 21.

** Process exited - Return Code: 0 **
Press Enter to exit terminal

Complex lambda function

It prints a range of values. Then for a key to get it sorted, I have used the lambda function.

my_list = range(-3,3)
my_list = sorted(my_list, key=lambda x: x*x)


The range values [-3, -2, -1, 0, 1, 2] sorted based on the key. You can see the output and how it’s sorted.

range(-3, 3)
[0, -1, 1, -2, 2, -3]
** Process exited - Return Code: 0 **
Press Enter to exit terminal

How to use for loop with lambda

Here’s an example python lambda for for-loop in python.

def my_add(y):
    return lambda: y
list = [my_add(i) for i in range(1,11)]
for z in list:


Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.