What is a Lambda Function in Python?

A lambda function in Python is an anonymous function (i.e., a function without a name) that is defined using the lambda keyword. It is mainly used for small, one-time operations where defining a full function is unnecessary.

Why Use Lambda Functions?

Concise & Readable – Reduces code size for simple functions
Faster Execution – Ideal for quick, one-time calculations
Useful in Functional Programming – Works well with map(), filter(), and reduce()

Basic Syntax of Lambda Functions

lambda arguments: expression
  • lambda – Declares the lambda function
  • arguments – Input parameters (can be multiple)
  • expression – The single operation performed (no return statement required)

Step-by-Step Guide with Examples for Python lambda

1. Creating a Simple Lambda Function

Example: Add Two Numbers

add = lambda x, y: x + y
print(add(5, 3))  # Output: 8

💡 Best Practice: Use lambda functions only for simple, one-liner operations.

2. Using Lambda Functions in map()

Example: Square Each Number in a List

numbers = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x**2, numbers))
print(squared)  # Output: [1, 4, 9, 16, 25]

💡 Best Practice: Use map() when applying the function to each element in a list.


3. Using Lambda Functions in filter()

Example: Filter Even Numbers from a List

numbers = [1, 2, 3, 4, 5, 6]
evens = list(filter(lambda x: x % 2 == 0, numbers))
print(evens)  # Output: [2, 4, 6]

💡 Best Practice: Use filter() when you need to select specific elements based on a condition.


4. Using Lambda Functions in reduce()

Example: Find the Product of a List of Numbers

from functools import reduce

numbers = [1, 2, 3, 4]
product = reduce(lambda x, y: x * y, numbers)
print(product)  # Output: 24

💡 Best Practice: Use reduce() only when you need to perform cumulative operations.


5. Using Lambda Functions with Sorting (sorted())

Example: Sort a List of Tuples by the Second Element

pairs = [(1, 3), (2, 2), (4, 1)]
sorted_pairs = sorted(pairs, key=lambda x: x[1])
print(sorted_pairs)  # Output: [(4, 1), (2, 2), (1, 3)]

💡 Best Practice: Use lambda functions as sorting keys when sorting complex data structures.


6. Using Lambda Functions for Conditional Expressions

Example: Check if a Number is Positive, Negative, or Zero

check_number = lambda x: "Positive" if x > 0 else ("Negative" if x < 0 else "Zero")
print(check_number(-5))  # Output: Negative

💡 Best Practice: Use lambda functions for simple if-else expressions, but avoid complex conditions.

Lambda Functions vs Regular Functions

FeatureLambda FunctionRegular Function
Definitionlambda x: x + 2def add(x): return x + 2
Return TypeImplicitExplicit (return)
ReadabilityCompact for simple logicBetter for complex logic
PerformanceSlightly faster for small tasksMore maintainable for large tasks

💡 Best Practice: Use lambda for short, simple operations. For complex logic, use a regular function.

When to Use Lambda Functions?

Short one-liner functions (e.g., simple calculations)
Used once in higher-order functions (e.g., map(), filter())
When defining functions inline (e.g., sorting custom keys)

Avoid when logic is complex – Use a regular def function instead.