Sitemap
6 min readOct 22, 2026

--

Understanding Lambda Functions in Python: Syntax, Examples, and Uses

When I started learning Python, I was familiar with defining functions using the def keyword. But when I came across lambda functions, I wondered why we needed another way to create functions when regular functions were already doing the job. Initially, the syntax seemed a little confusing, especially when lambda functions were used with map(), filter(), and reduce(). So, I decided to explore this concept in detail and understand not just how lambda functions work, but also when and why we should use them.

Press enter or click to view image in full size
Photo by Emile Perron on Unsplash

Introduction

Python is known for its simple syntax and readability. One of its interesting features is the lambda function, which allows us to create small functions in a single line of code.
When learning Python, we usually begin by defining functions using the ‘def’ keyword. These functions help us organize code, reuse logic, and perform specific tasks. However, Python also provides another way to create functions using the ‘lambda’ keyword.

For example, suppose we want to calculate the square of a number.

We can write a regular function like this:

def square(n):
return n * n
print(square(5))

Output:
25

But we can write the same logic using a lambda function:

square = lambda n: n * n
print(square(5))

Output:
25

Both approaches produce the same result, but their syntax is different.

This raises some interesting questions:

  • What exactly is a lambda function?
    - How does it work internally?
    - What is the difference between ‘lambda’ and ‘def’?
    - Why do we use lambda functions with map(), filter(), and reduce()?
    - When should we use lambda functions, and when should we avoid them?
    In this article, we will explore these questions step by step, starting with the fundamentals and gradually moving toward practical examples.

What Is a Lambda Function in Python?

A lambda function is a small anonymous function that is defined using the ‘lambda’ keyword.

Let’s understand the important terms in this definition.

Anonymous function: A function created without a conventional function name. Although a lambda expression can be assigned to a variable, the function itself does not have a name assigned through a ‘def’ declaration.

Single expression: A lambda function contains one expression whose result becomes the function’s return value.

For example:

lambda x: x + 10
This lambda function accepts a parameter ‘x’ and adds ‘10’ to it.

However, simply writing the expression does not mean we have called the function. To execute it, we need to call the resulting function object.

One way to do that is to assign it to a variable:
add_ten = lambda x: x + 10

print(add_ten(5))
print(add_ten(20))

Output:
15
30

Here is what happens:

1. lambda x: x + 10 creates a function.
2. add_ten refers to that function.
3. When we write add_ten(5), the value ‘5’ is passed to ‘x’.
4. Python evaluates ‘x + 10’.
5. The result is returned automatically.

Notice that we did not write a ‘return’ statement. Lambda functions automatically return the value of their expression.

Why do we need lambda functions?
They allow us to write a function in a single line, making the code more concise and convenient.
For example, if we want to square a number or check whether a number is even, we can use a lambda function instead of creating a separate function for that task.

Understanding the Syntax of a Lambda Function

The general syntax is:

lambda arguments: expression

A lambda function consists of three main parts:

‘lambda’: The keyword used to create the function.
‘arguments’: The parameters that receive input values.
‘expression’: The operation whose result is returned.

Lambda Functions vs. Regular Functions

To understand lambda functions properly, we should compare them with regular functions.

Example 1: Using `def`

def add(a, b):
return a + b
print(add(10, 20))

Output:
30

Example 2: Using ‘lambda’

add = lambda a, b: a + b
print(add(10, 20))

Output:
30

Both functions behave the same way for these inputs.

However, they differ in how they are defined and what kinds of code they can contain.
A regular function is defined using the def keyword and is suitable for complex operations and reusable code. A lambda function is defined using the lambda keyword and is used for small, simple operations in a single expression. Regular functions can contain multiple statements, while lambda functions are limited to a single expression.

Lambda Functions With Multiple Arguments

A common misconception is that lambda functions can accept only one argument. That is incorrect.

A lambda function can accept zero or more parameters, subject to Python’s usual parameter rules.

Example 1: Finding the maximum of two numbers

maximum = lambda a, b: a if a > b else b
print(maximum(15, 9))

Output:
15

Understanding Lambda Functions With map()

Now we come to one of the most useful applications of lambda functions.

Get Priyamaurya’s stories in your inbox

Join Medium for free to get updates from this writer.

The ‘map()’ function applies a given function to every item in an iterable, such as a list or tuple.

Syntax: map(function, iterable)

Here:
- ‘function’ is the operation to apply.
- ‘iterable’ contains the values to process.
- ‘map()’ returns an iterator in Python 3.

Example 1: Square every number in a list

numbers = [1, 2, 3, 4, 5]
result = list(map(lambda x: x * x, numbers))
print(result)

Output:
[1, 4, 9, 16, 25]

Example 2: Convert strings to integers

numbers = [“10”, “20”, “30”, “40”]
result = list(map(lambda x: int(x), numbers))
print(result)

Output:
[10, 20, 30, 40]

Understanding Lambda Functions With filter()

The ‘filter()’ function selects items from an iterable based on a condition.

Syntax: filter(function, iterable)

The function should return a truthy or falsy result for each item. Only the items whose results are truthy are retained.

Example 1: Filter even numbers

numbers = [1, 2, 3, 4, 5, 6, 7, 8]
result = list(filter(lambda x: x % 2 == 0, numbers))
print(result)

Output:
[2, 4, 6, 8]

Example 2: Filter words that start with a particular letter

words = [“apple”, “banana”, “avocado”, “mango”, “apricot”]
result = list(filter(lambda word: word.startswith(“a”), words))
print(result)

Output:
[‘apple’, ‘avocado’, ‘apricot’]

Lambda Functions With ‘reduce()’

reduce() combines the elements of an iterable into a single result by repeatedly applying a function.
The reduce() function is available in the ‘functools’ module.

Syntax: reduce(function, iterable)

Example 1: Calculate the sum of all numbers

from functools import reduce
numbers = [1, 2, 3, 4, 5]
result = reduce(lambda a, b: a + b, numbers)
print(result)

Output:
15

How does it work?
The lambda accepts two values, a and b, and adds them.

reduce() performs the following steps:

1. Add 1 and 2 to get ‘3’.
2. Add the result 3 and the next number 3 to get ‘6’.
3. Add 6 and 4 to get ‘10’.
4. Add 10 and 5 to get ‘15’.

The final result is 15.

Can Lambda Functions Have Default Arguments?

Yes. Lambda functions support default arguments just like regular functions.

For example:

greet = lambda name=”Guest”: “Hello, “ + name

print(greet())
print(greet(“Priya”))

Output:

Hello, Guest
Hello, Priya

When no argument is provided, the default value ‘Guest’ is used.

When ‘Priya’ is provided, it replaces the default value.

What Lambda Functions Cannot Do

Understanding the limitations of lambda functions is just as important as understanding their uses.

1. They cannot contain multiple statements in their body

lambda x:
y = x + 2
y * 3

This is invalid because assignment is a statement, and the lambda body must be an expression.

2. They cannot contain ‘return’ statements

lambda x: return x * 2

A lambda returns the value of its expression automatically.

Correct:

double = lambda x: x * 2
print(double(5))

Output:
10

3. They are not ideal for complex logic

You can write complicated expressions using a lambda, but that doesn’t mean you should.
For example, nested conditional expressions might technically work, but they can make code difficult to maintain.
A regular function is often a better choice when logic requires multiple steps, meaningful variable names, exception handling, or detailed documentation.

And that’s it!! I hope this article helped you understand Python lambda functions and when to use them.

--

--