Introduction
If you’ve ever seen the word lambda in Python code and wondered what it’s doing — you’re in the right place.
In this post, we’ll explore what is lambda function in Python, how to use it, and why it’s helpful for writing short, quick operations.
This guide is designed for beginners who want clear explanations, small code samples, and real-life examples that make learning fun.
TL;DR
A lambda function in Python is a small, unnamed (anonymous) function that’s written in a single line.
It’s useful when you need a quick function for sorting, filtering, or mapping data — without using def.
Table of Contents
What Is a Lambda Function in Python?
A lambda function is just a quick, one-line way to create a function without giving it a name.
Instead of writing a full def block, you can define and use it immediately.

Syntax of lambda function in Python:
lambda arguments: expression
Run this using: python3 filename.py or Jupyter Notebook
Example:
# Simple lambda function example
add = lambda x, y: x + y
print(add(5, 7))
Output
12
Note: Lambda functions can have any number of arguments, but they only contain one expression.
Why We Use Lambda Function in Python
There are moments when writing a full function feels like too much work.
That’s when lambda functions come in handy.
Here’s are the main reasons:
- To make the code shorter and cleaner
- When passing small functions to other functions (like
map(),filter(), orsorted()) - For quick calculations or transformations inside data operations
Personal Tip: I usually use lambda functions when I need a temporary function — for example, while filtering lists or sorting tuples by a specific value.
How to Use Lambda Function in Python
Let’s look at some practical examples to understand this better.

Example 1: Lambda with map()
numbers = [1, 2, 3, 4, 5]
squares = list(map(lambda x: x**2, numbers))
print(squares)
Output:
[1, 4, 9, 16, 25]
Explanation:
Here, the map() function applies the lambda expression to each element in the list numbers.
The lambda function lambda x: x**2 means: “take a number x and return its square.”
So Python goes through each number — 1, 2, 3, 4, 5 — and replaces it with its square.
The result is a new list [1, 4, 9, 16, 25].
Personal Tip:
Use map() with lambda when you need to modify or transform every item in a list quickly — like converting temperatures, prices, or numerical values.
Example 2: Lambda with filter()
numbers = [10, 15, 21, 30, 35]
divisible_by_5 = list(filter(lambda x: x % 5 == 0, numbers))
print(divisible_by_5)
Output:
[10, 15, 30, 35]
Explanation:
The filter() function keeps only the elements that satisfy a condition.
Here, our lambda checks if each number is divisible by 5.
If the condition x % 5 == 0 is True, that number stays in the new list.
So 10, 15, 30, and 35 pass the test, while 21 is removed.
Practical Example:
This trick is super useful when you want to filter data — like selecting even numbers, active users, or items above a certain price.
Example 3: Lambda Function in Python List Sorting
students = [("Vaibhav", 85), ("Anita", 92), ("Rahul", 78)]
sorted_students = sorted(students, key=lambda x: x[1], reverse=True)
print(sorted_students)
Output:
[('Anita', 92), ('Vaibhav', 85), ('Rahul', 78)]
Explanation:
The sorted() function can sort complex data — here, a list of tuples.
The lambda tells Python to sort using the second item in each tuple (the marks).
The key=lambda x: x[1] part means: “use the second value as the sorting key.”
We also set reverse=True to get descending order — highest marks first.
Tip:
Lambda functions are perfect for quick sorting tasks — like ranking scores, sorting by age, or organizing names alphabetically.
Example 4: Inline Lambda Function
print((lambda x, y: x * y)(6, 3))
Output:
18
Explanation:
Here we’re using a lambda inline, without assigning it to a variable.
The expression (lambda x, y: x * y)(6, 3) defines and immediately calls the function — multiplying 6 and 3.
This is handy for one-time calculations or quick operations inside other code (like data pipelines).
Personal Note:
Use inline lambdas for small math or transformation tasks where writing a full function would be overkill.
Lambda Function Syntax in Python
Here’s a simple breakdown of the syntax
| Component | Meaning |
|---|---|
lambda | Keyword used to define the anonymous function |
arguments | Inputs or parameters to the function |
expression | The operation or result returned by the function |
Examples:
multiply = lambda a, b: a * b
print(multiply(4, 2))
Output:
8
Note: Lambda automatically returns the value of its expression — you don’t need to use return.
What Is the Purpose of Lambda Function in Python
The main purpose of lambda function is to create short, temporary functions for simple operations.
They’re especially useful when you:
- Don’t want to define a full
deffunction - Need quick inline logic
- Work with functions like
map(),filter(), orreduce()
Reference: Official Python Documentation — Lambda Expressions
FAQs About Lambda Function in Python
Q1. What is lambda function in Python with example?
A lambda function is a small, unnamed function used for one-line operations. Example:lambda x: x + 5 adds 5 to any number.
Q2. Why we use lambda function in Python?
To perform short, quick tasks like sorting or filtering without defining a regular function.
Q3. What is the syntax of lambda function in Python?lambda arguments: expression — it’s one line, and the result is automatically returned.
Q4. Can lambda functions be used with lists?
Yes! You can use lambda with map() or filter() to process list items efficiently.
Q5. What is the purpose of lambda function in Python?
To make your code compact and easier to read for small, one-time use cases.
Next Learning Step
Want to keep learning? Here are some related Python tutorials:
Author Note: Written by Vaibhav Kurale, a Python blogger who loves turning complex coding ideas into simple, practical examples.