Python Lambda Functions Syntax, Usage, Examples

What is a Lambda Function?

A lambda function is a small, anonymous function defined using the lambda keyword instead of def. Unlike regular functions, lambda functions don’t have a name and are generally used for short, throwaway operations.

Basic Syntax

The basic syntax of a lambda function in Python is:

lambda arguments: expression

A lambda function can take any number of arguments, but it must contain only a single expression. It returns the result of that expression automatically — no need to use the return keyword.

Example: Add Two Numbers

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

Explanation:

  • lambda x, y: defines an anonymous function that takes two parameters: x and y.
  • The expression x + y is evaluated and returned as the result of the function.
  • This lambda function is assigned to the variable add, so you can call it like a regular function using add(5, 3).
  • When called with arguments 5 and 3, it returns 8, which is printed to the output.

Sorting with Lambda

Lambda functions are particularly useful when passed as arguments to higher-order functions. Consider the example below where we sort a list of tuples by the second element:

data = [(1, 4), (2, 1), (3, 9)]
sorted_data = sorted(data, key=lambda x: x[1])
print(sorted_data)
[(2, 1), (1, 4), (3, 9)]

Filtering with Lambda

Use lambda with filter() to select only even numbers from a list:

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

Mapping with Lambda

Apply a function to every element using map():

nums = [1, 2, 3, 4]
squares = list(map(lambda x: x**2, nums))
print(squares)
[1, 4, 9, 16]

When Should You Use Lambda?

  • When the function is short and simple.
  • When the function will not be reused elsewhere.
  • When you need a function as an argument (e.g., in map, filter, sorted).

When NOT to Use Lambda

Lambda functions come with limitations. Avoid them when:

  • The logic inside is too complex.
  • You want to reuse or debug the function later.
  • You need statements (lambda only allows expressions).

Common Mistakes and Checks

  • Only one expression is allowed. Statements like if/else, try/except, and print() inside lambda are not allowed unless used as expressions.
  • Readability matters. Just because you can use a lambda doesn’t mean you should. Avoid sacrificing clarity.
  • Debugging is hard. Anonymous nature makes it difficult to trace errors.

Example: Conditional Expression in Lambda

max_func = lambda a, b: a if a > b else b
print(max_func(10, 20))
20

Lambda vs Regular Function

def add_def(x, y):
    return x + y

add_lambda = lambda x, y: x + y

print(add_def(2, 3))    # Output: 5
print(add_lambda(2, 3)) # Output: 5
5
5

How to Test and Verify

  • Always start with small examples. Try printing the output after defining a lambda.
  • Use type() to confirm it's a function: print(type(lambda x: x))
  • Compare behavior side-by-side with a regular function to understand what’s happening.

What is a Lambda Function?

A lambda function is a small, anonymous function defined using the lambda keyword instead of def. Unlike regular functions, lambda functions don’t have a name and are generally used for short, throwaway operations.

Basic Syntax

The basic syntax of a lambda function in Python is:

lambda arguments: expression

A lambda function can take any number of arguments, but it must contain only a single expression. It returns the result of that expression automatically — no need to use the return keyword.

Example: Add Two Numbers

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

Explanation:

  • lambda x, y: defines an anonymous function that takes two parameters: x and y.
  • The expression x + y is evaluated and returned as the result of the function.
  • This lambda function is assigned to the variable add, so you can call it like a regular function using add(5, 3).
  • When called with arguments 5 and 3, it returns 8, which is printed to the output.

Sorting with Lambda

Lambda functions are particularly useful when passed as arguments to higher-order functions. Consider the example below where we sort a list of tuples by the second element:

data = [(1, 4), (2, 1), (3, 9)]
sorted_data = sorted(data, key=lambda x: x[1])
print(sorted_data)
[(2, 1), (1, 4), (3, 9)]

Filtering with Lambda

Use lambda with filter() to select only even numbers from a list:

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

Mapping with Lambda

Apply a function to every element using map():

nums = [1, 2, 3, 4]
squares = list(map(lambda x: x**2, nums))
print(squares)
[1, 4, 9, 16]

When Should You Use Lambda?

  • When the function is short and simple.
  • When the function will not be reused elsewhere.
  • When you need a function as an argument (e.g., in map, filter, sorted).

When NOT to Use Lambda

Lambda functions come with limitations. Avoid them when:

  • The logic inside is too complex.
  • You want to reuse or debug the function later.
  • You need statements (lambda only allows expressions).

Common Mistakes and Checks

  • Only one expression is allowed. Statements like if/else, try/except, and print() inside lambda are not allowed unless used as expressions.
  • Readability matters. Just because you can use a lambda doesn’t mean you should. Avoid sacrificing clarity.
  • Debugging is hard. Anonymous nature makes it difficult to trace errors.

Example: Conditional Expression in Lambda

max_func = lambda a, b: a if a > b else b
print(max_func(10, 20))
20

Lambda vs Regular Function

def add_def(x, y):
    return x + y

add_lambda = lambda x, y: x + y

print(add_def(2, 3))    # Output: 5
print(add_lambda(2, 3)) # Output: 5
5
5

How to Test and Verify

  • Always start with small examples. Try printing the output after defining a lambda.
  • Use type() to confirm it's a function: print(type(lambda x: x))
  • Compare behavior side-by-side with a regular function to understand what’s happening.

Comments

💬 Please keep your comment relevant and respectful. Avoid spamming, offensive language, or posting promotional/backlink content.
All comments are subject to moderation before being published.


Loading comments...