Learning Guides
Lambda Function in Python: Complete Guide with Examples

Python provides multiple ways to create functions. While most functions are created using the def keyword, Python also offers a shorter and more concise way of defining simple functions called Lambda Functions.
Lambda functions are especially useful when you need a small function for a short period of time and don't want to define a complete function using def.
In this guide, you'll learn:
What Lambda Functions are
Syntax of Lambda Functions
Lambda vs Normal Functions
Lambda with map()
Lambda with filter()
Lambda with reduce()
Real-world use cases
Advantages and limitations
Interview questions
What is a Lambda Function in Python?
A Lambda Function is an anonymous function that can have any number of arguments but only one expression.
Unlike normal functions, lambda functions do not require a name.
Example:
square = lambda x: x * x
print(square(5))
Output:
25
Why are Lambda Functions Used?
Lambda functions are useful when:
Function logic is simple
Function is used only once
Code readability can be improved
Functional programming techniques are required
They help reduce unnecessary code.
Syntax of Lambda Function
General syntax:
lambda arguments: expression
Example:
add = lambda a, b: a + b
print(add(10, 20))
Output:
30
Lambda Function vs Normal Function
Normal Function
def square(x):
return x * x
print(square(5))
Lambda Function
square = lambda x: x * x
print(square(5))
Both produce the same result.
Lambda Function with Multiple Arguments
Example:
multiply = lambda a, b, c: a * b * c
print(
multiply(2, 3, 4)
)
Output:
24
Lambda Function with Conditional Expressions
Example:
check = lambda x: "Even" if x % 2 == 0 else "Odd"
print(check(8))
Output:
Even
Lambda Function with map()
The map() function applies a function to every element in an iterable.
Example:
numbers = [1, 2, 3, 4]
result = list(
map(
lambda x: x * 2,
numbers
)
)
print(result)
Output:
[2, 4, 6, 8]
Lambda Function with filter()
The filter() function selects elements that satisfy a condition.
Example:
numbers = [1, 2, 3, 4, 5, 6]
result = list(
filter(
lambda x: x % 2 == 0,
numbers
)
)
print(result)
Output:
[2, 4, 6]
Lambda Function with reduce()
The reduce() function performs cumulative operations.
Example:
from functools import reduce
result = reduce(
lambda a, b: a + b,
[1, 2, 3, 4]
)
print(result)
Output:
10
Lambda Function with sorted()
Lambda functions are commonly used as sorting keys.
Example:
students = [
("John", 85),
("Emma", 95),
("David", 75)
]
sorted_students =
sorted(
students,
key=lambda x: x[1]
)
print(sorted_students)
Output:
[
('David', 75),
('John', 85),
('Emma', 95)
]
Lambda Function with Lists
Example:
numbers = [10, 20, 30]
result = list(
map(
lambda x: x + 5,
numbers
)
)
print(result)
Output:
[15, 25, 35]
Lambda Function with Dictionary Data
Example:
employees = {
"Alice": 50000,
"Bob": 60000,
"Charlie": 45000
}
highest =
max(
employees,
key=lambda x:
employees[x]
)
print(highest)
Output:
Bob
Real-World Applications of Lambda Functions
Lambda functions are widely used in:
Data Science
Applications:
Data Cleaning
Feature Engineering
Data Transformation
Example:
map()
filter()
Machine Learning
Applications:
Feature Selection
Dataset Processing
Data Preparation
Web Development
Applications:
Data Formatting
Validation Logic
API Response Processing
Automation
Applications:
File Processing
Log Analysis
Data Extraction
Advantages of Lambda Functions
Benefits include:
Shorter Code
Reduces unnecessary lines.
Better Readability
For simple operations.
Functional Programming Support
Works seamlessly with:
map()
filter()
reduce()
Quick Implementation
Ideal for one-time functions.
Limitations of Lambda Functions
Despite their usefulness, lambda functions have limitations.
Single Expression Only
Lambda functions cannot contain multiple statements.
Invalid example:
lambda x:
print(x)
return x
Less Readable for Complex Logic
For larger functions, normal functions are preferred.
Cannot Include Multiple Statements
Loops and extensive logic are not suitable.
Common Lambda Function Interview Questions
What is a Lambda Function?
A lambda function is an anonymous function that contains a single expression.
Why Use Lambda Functions?
They simplify small and temporary functions.
Can Lambda Functions Have Multiple Arguments?
Yes.
Example:
lambda a, b: a + b
Difference Between Lambda and Def Function?
| Lambda Function | Def Function |
|---|---|
| Anonymous | Named |
| Single Expression | Multiple Statements |
| Short Syntax | Longer Syntax |
Can Lambda Functions Return Values?
Yes.
The expression result is automatically returned.
Best Practices
Use Lambda for Small Operations
Keep logic concise and simple.
Avoid Complex Expressions
Use regular functions for complex workflows.
Combine with map() and filter()
Lambda functions are most effective when paired with functional programming tools.
Improve Readability
If a lambda becomes difficult to understand, replace it with a normal function.
Frequently Used Lambda Examples
Square Number
lambda x: x * x
Add Two Numbers
lambda a, b: a + b
Find Maximum
lambda a, b:
a if a > b else b
Check Even Number
lambda x:
x % 2 == 0
Final Thoughts
Lambda Functions are one of Python's most useful features for writing concise and efficient code. They provide a quick way to define anonymous functions and are commonly used with map(), filter(), reduce(), and sorting operations.
While lambda functions are excellent for simple tasks, larger and more complex logic should still be implemented using standard functions with the def keyword. Understanding when and where to use lambda functions is an important skill for Python developers, Data Analysts, Data Scientists, and Machine Learning Engineers.
Keep Reading
Related Articles
Learning Guides
Introduction to Computer Vision: A Beginner’s Guide
Discover the basics of Computer Vision, a rapidly growing field of Artificial Intelligence that enables machines to understand images and videos. Expl
All About PyCaret: Conversation with Mr. Moez Ali and Mr. Aniruddha Kalbande | EP-06
Explore PyCaret and its impact on machine learning development through an insightful conversation with PyCaret creator Moez Ali and AI expert Aniruddh
What is Data Mining? Complete Guide for Beginners
Learn what Data Mining is, how it works, key techniques, tools, applications, advantages, challenges, and its role in Data Science, Machine Learning,