By ATS Staff - February 18th, 2026
Python Programming Software Development
In Python, lambda functions are small, anonymous functions defined in a single line. They are commonly used for short, simple operations where defining a full function using def would be unnecessary.
Lambda functions make your code more concise and are especially useful when working with higher-order functions like map(), filter(), and sorted().
A lambda function is an anonymous function — meaning it doesn’t have a name (unless you assign it to a variable).
lambda arguments: expression
lambda → keyword to define the functionarguments → input parametersexpression → a single expression that is evaluated and returnedUnlike normal functions:
def add(x, y):
return x + y
add = lambda x, y: x + y
Both work the same way:
print(add(5, 3)) # Output: 8
Lambda functions are useful when:
map()The map() function applies a function to every item in an iterable.
numbers = [1, 2, 3, 4] squared = list(map(lambda x: x**2, numbers)) print(squared)
Output:
[1, 4, 9, 16]
filter()The filter() function filters elements based on a condition.
numbers = [1, 2, 3, 4, 5, 6] even = list(filter(lambda x: x % 2 == 0, numbers)) print(even)
Output:
[2, 4, 6]
sorted()Lambda functions are commonly used as a key function in sorting.
students = [
("Ali", 85),
("Sara", 92),
("John", 78)
]
sorted_students = sorted(students, key=lambda student: student[1])
print(sorted_students)
This sorts students by marks.
Lambda functions can take multiple arguments:
multiply = lambda a, b, c: a * b * c print(multiply(2, 3, 4)) # Output: 24
You can return a lambda function from another function:
def power(n):
return lambda x: x ** n
square = power(2)
cube = power(3)
print(square(5)) # 25
print(cube(5)) # 125
This demonstrates closures in Python.
Lambda functions:
For complex functionality, always use a normal def function.
Avoid lambda functions when:
Lambda functions in Python provide a quick and efficient way to define small anonymous functions. They are powerful when used correctly — especially with functional programming tools like map(), filter(), and sorted().
However, clarity should always come first. Use lambda functions for simplicity, not complexity.