---
title: "Explain the Python Lambda with example."  
description: "Explain the Python Lambda with example."  
author: "ICSM Computer"  
published: 2025-09-29  
updated: 2025-10-01  
canonical: https://www.mindstick.com/forum/161931/explain-the-python-lambda-with-example  
category: "python"  
tags: ["python-3.4", "Python 3"]  
reading_time: 2 minutes  

---

# Explain the Python Lambda with example.

**[Explain](https://www.mindstick.com/forum/157854/what-is-system-debugging-explain-some-system-debugging-tools-used-in-modern-computer-systems) the [Python](https://www.mindstick.com/articles/75378/simple-yet-useful-tips-when-using-python) [Lambda](https://www.mindstick.com/blog/181/lambda-expression-in-c-sharp) with example.**

## Replies

### Reply by Anubhav Sharma

> A `lambda` is a **small, anonymous function** (a function without a name). It can have **any number of arguments**, but only **one expression**.

## Syntax:

It’s like a **shortcut** for defining functions you only need once.

### [Normal function](https://www.mindstick.com/forum/161928/explain-the-python-functions-with-explanation):

```python
def add(x, y):
    return x + y

print(add(5, 3))  # 8
```

### Lambda equivalent:

```python
add = lambda x, y: x + y
print(add(5, 3))  # 8
```

Both work the same way, but the lambda is shorter.

## 1. Common Uses of Lambda

### a) With `map()` – apply function to each item

```python
nums = [1, 2, 3, 4]
squares = list(map(lambda x: x**2, nums))
print(squares)  # [1, 4, 9, 16]
```

### b) With `filter()` – filter items

```python
nums = [1, 2, 3, 4, 5, 6]
evens = list(filter(lambda x: x % 2 == 0, nums))
print(evens)  # [2, 4, 6]
```

### c) With `sorted()` – custom sorting

```python
words = ["apple", "banana", "cherry", "kiwi"]
sorted_words = sorted(words, key=lambda w: len(w))
print(sorted_words)  # ['kiwi', 'apple', 'banana', 'cherry']
```

### d) Inline quick calculations

```python
double = lambda x: x * 2
print(double(10))  # 20
```

## 2. Limitations of Lambda

- Only **one expression** (no multiple statements).
- Not good for **complex logic** → better to use `def`.
- Used mostly for **short, throwaway functions**.

## In short:

- `lambda` = anonymous, single-expression function.
- Great with `map()`, `filter()`, `reduce()`, `sorted()`.
- Keep it for **small tasks**, use `def` for larger functions.


---

Original Source: https://www.mindstick.com/forum/161931/explain-the-python-lambda-with-example

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
