Lesson 37 of 70 – Lambda Functions
53%

Python Lambda Functions

A lambda function is a small anonymous function in Python. It can take any number of arguments but can have only one expression.

Note: Lambda functions are useful when you need a small function for a short period of time.
What is a Lambda Function?

A lambda function is a function created using the lambda keyword instead of the def keyword.

Basic syntax:

lambda arguments : expression

The expression is evaluated and its result is automatically returned.

Basic Lambda Example
square = lambda x: x * x

print(square(5))
Output:
25

Here, x is the parameter and x * x is the expression.

Lambda 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 functions produce the same result. The lambda version is shorter.

Lambda with One Argument
double = lambda x: x * 2

print(double(10))
Output:
20
Lambda with Two Arguments
add = lambda a, b: a + b

print(add(10, 20))
Output:
30
Lambda with Multiple Arguments

A lambda function can accept multiple arguments.

calculate = lambda a, b, c: a + b + c

print(calculate(10, 20, 30))
Output:
60
Lambda with Subtraction
subtract = lambda a, b: a - b

print(subtract(20, 8))
Output:
12
Lambda with Multiplication
multiply = lambda a, b: a * b

print(multiply(5, 6))
Output:
30
Lambda with Division
divide = lambda a, b: a / b

print(divide(20, 4))
Output:
5.0
Lambda with Conditional Expression

A lambda function can contain a conditional expression.

check = lambda x: "Even" if x % 2 == 0 else "Odd"

print(check(10))
print(check(7))
Output:
Even
Odd
Lambda for Finding Maximum
maximum = lambda a, b: a if a > b else b

print(maximum(15, 10))
Output:
15
Lambda with Strings
length = lambda text: len(text)

print(length("Python"))
Output:
6
Lambda with Lists

Lambda functions can also be used with list elements.

numbers = [1, 2, 3, 4, 5]

square = lambda x: x * x

for n in numbers:
    print(square(n))
Output:
1
4
9
16
25
Lambda with map()

The map() function applies a function to every item in an iterable. Lambda functions are commonly used with map().

numbers = [1, 2, 3, 4, 5]

squares = list(map(lambda x: x * x, numbers))

print(squares)
Output:
[1, 4, 9, 16, 25]
Lambda with filter()

The filter() function selects items 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]
Lambda with sorted()

Lambda functions can be used as a key for sorting.

students = [
    ("Amit", 75),
    ("Ravi", 90),
    ("Neha", 82)
]

result = sorted(students, key=lambda x: x[1])

print(result)
Output:
[('Amit', 75), ('Neha', 82), ('Ravi', 90)]

Here, x[1] represents the marks of each student.

Lambda with sorted() in Descending Order
students = [
    ("Amit", 75),
    ("Ravi", 90),
    ("Neha", 82)
]

result = sorted(
    students,
    key=lambda x: x[1],
    reverse=True
)

print(result)
Output:
[('Ravi', 90), ('Neha', 82), ('Amit', 75)]
Lambda as a Function Argument

A lambda function can be passed as an argument to another function.

def calculate(func, number):
    return func(number)

result = calculate(lambda x: x * 2, 10)

print(result)
Output:
20
Lambda with Multiple Operations
calculate = lambda a, b: (a + b) * 2

print(calculate(5, 10))
Output:
30
Important Rules of Lambda Functions
  • Lambda functions use the lambda keyword.
  • They are anonymous functions.
  • They can accept multiple arguments.
  • They contain only one expression.
  • The result of the expression is returned automatically.
  • They are useful for short and simple operations.
  • They are frequently used with map(), filter() and sorted().
Lambda Function Example
numbers = [10, 15, 20, 25, 30]

greater_than_20 = list(
    filter(lambda x: x > 20, numbers)
)

print(greater_than_20)
Output:
[25, 30]
Advantages of Lambda Functions
  • They make small functions shorter.
  • They are useful for temporary operations.
  • They work well with functions such as map() and filter().
  • They can make simple sorting operations easier.
  • They are useful when a named function is unnecessary.
Limitations of Lambda Functions
  • A lambda function contains only one expression.
  • Complex logic can become difficult to read.
  • Large functions should generally use def.
  • Lambda functions are not always easier to understand.
Key Points
  • Lambda creates a small anonymous function.
  • The syntax is lambda arguments: expression.
  • A lambda can accept multiple arguments.
  • A lambda function contains one expression.
  • The expression result is returned automatically.
  • Lambda functions are commonly used with map(), filter() and sorted().
  • Use normal functions with def when the logic becomes complex.

🧠 Quick Quiz

Question: Which keyword is used to create a lambda function in Python?