
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
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
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.
General syntax:
lambda arguments: expression
Example:
add = lambda a, b: a + b
print(add(10, 20))
Output:
30
def square(x):
return x * x
print(square(5))
square = lambda x: x * x
print(square(5))
Both produce the same result.
Example:
multiply = lambda a, b, c: a * b * c
print(
multiply(2, 3, 4)
)
Output:
24
Example:
check = lambda x: "Even" if x % 2 == 0 else "Odd"
print(check(8))
Output:
Even
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]
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]
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 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)
]
Example:
numbers = [10, 20, 30]
result = list(
map(
lambda x: x + 5,
numbers
)
)
print(result)
Output:
[15, 25, 35]
Example:
employees = {
"Alice": 50000,
"Bob": 60000,
"Charlie": 45000
}
highest =
max(
employees,
key=lambda x:
employees[x]
)
print(highest)
Output:
Bob
Lambda functions are widely used in:
Applications:
Data Cleaning
Feature Engineering
Data Transformation
Example:
map()
filter()
Applications:
Feature Selection
Dataset Processing
Data Preparation
Applications:
Data Formatting
Validation Logic
API Response Processing
Applications:
File Processing
Log Analysis
Data Extraction
Benefits include:
Reduces unnecessary lines.
For simple operations.
Works seamlessly with:
map()
filter()
reduce()
Ideal for one-time functions.
Despite their usefulness, lambda functions have limitations.
Lambda functions cannot contain multiple statements.
Invalid example:
lambda x:
print(x)
return x
For larger functions, normal functions are preferred.
Loops and extensive logic are not suitable.
A lambda function is an anonymous function that contains a single expression.
They simplify small and temporary functions.
Yes.
Example:
lambda a, b: a + b
| Lambda Function | Def Function |
|---|---|
| Anonymous | Named |
| Single Expression | Multiple Statements |
| Short Syntax | Longer Syntax |
Yes.
The expression result is automatically returned.
Keep logic concise and simple.
Use regular functions for complex workflows.
Lambda functions are most effective when paired with functional programming tools.
If a lambda becomes difficult to understand, replace it with a normal function.
lambda x: x * x
lambda a, b: a + b
lambda a, b:
a if a > b else b
lambda x:
x % 2 == 0
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.