---
title: "The Amazing Fact Why Can’t We use Loops in Python Lambda"
description: "You can't use loops in the lambda function. Here're the reason explained with simple and complex examples."
url: https://srinimf.com/2022/05/30/amazing-fact-why-cant-we-use-for-in-python-lambda/
date: 2022-05-30
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2022/05/pexels-photo-269077.jpeg?fit=1880%2C1253&ssl=1
categories: ["Python"]
tags: ["python lambda"]
type: post
lang: en
---

# 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](https://i0.wp.com/srinimf.com/wp-content/uploads/2022/06/python-lambda.png?resize=500%2C500&ssl=1)*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)
print(myresult(7))
```

Output

The output is 21.

```
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)
print(my_list)
my_list = sorted(my_list, key=lambda x: x*x)
print(my_list)
```

Output

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](https://srinimf.com/2019/10/24/python-loop-control-statements-real-syntax/) 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:
    print(z())
```

Related

- [Python String Space Removal: 3 Efficient Methods](https://srinimf.com/2023/11/28/remove-spaces-in-string-in-python-3-approaches/)DateNovember 28, 2023
- [Python Filter Function on List: 4 Best Examples](https://srinimf.com/2023/03/27/python-how-to-filter-list-best-example/)DateMarch 27, 2023
- [Python: A Neat Way to Write Recursive Functions](https://srinimf.com/2023/08/05/how-to-write-recursive-functions-in-python/)DateAugust 5, 2023

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2022%2F05%2F30%2Famazing-fact-why-cant-we-use-for-in-python-lambda%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2022%2F05%2Fpexels-photo-269077.jpeg%3Ffit%3D1200%252C800%26ssl%3D1&description=The%20Amazing%20Fact%20Why%20Can%27t%20We%20use%20Loops%20in%20Python%20Lambda)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2022/05/30/amazing-fact-why-cant-we-use-for-in-python-lambda/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2022/05/30/amazing-fact-why-cant-we-use-for-in-python-lambda/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2022/05/30/amazing-fact-why-cant-we-use-for-in-python-lambda/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2022/05/30/amazing-fact-why-cant-we-use-for-in-python-lambda/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20The%20Amazing%20Fact%20Why%20Can%27t%20We%20use%20Loops%20in%20Python%20Lambda&body=https%3A%2F%2Fsrinimf.com%2F2022%2F05%2F30%2Famazing-fact-why-cant-we-use-for-in-python-lambda%2F&share=email)
-

### Like this:

Like Loading…
