62. Lambda Functions

Lambda Functions in Python

Lambda functions in Python are anonymous (unnamed) functions that are defined using the lambda keyword. They are usually one-liner functions that can accept any number of arguments but only have one expression.

The syntax for a lambda function is:

Copy

lambda arguments: expression

Where:

  • arguments are the input parameters (can be multiple, separated by commas).

  • expression is a single expression whose result is returned.

Lambda functions are often used in places where you need a small function for a short period and don't want to formally define a function using def.

1. Basic Example

A simple lambda function that adds two numbers:

Copy

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

In this example, lambda x, y: x + y is equivalent to the following function:

Copy

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

2. Lambda with Sorting

Lambda functions are frequently used with functions like sorted(), where you need to specify a custom sorting key.

For example, sorting a list of tuples based on the second element:

Copy

Output:

Copy

Here, the lambda function lambda x: x[1] tells sorted() to sort the tuples by the second element (the fruit name).

3. Using Lambda with map()

map() is a function that applies a function to all items in an iterable (like a list) and returns a map object (which is an iterator).

For example, multiplying each number in a list by 2:

Copy

4. Using Lambda with filter()

filter() is used to filter elements from an iterable based on a condition.

For example, filtering out even numbers from a list:

Copy

Here, the lambda function lambda x: x % 2 == 0 returns True for even numbers, which are included in the result.

5. Using Lambda with reduce()

reduce() is used to apply a binary function (a function that takes two arguments) cumulatively to the items in an iterable, from left to right, to reduce the iterable to a single value. It is available in the functools module.

For example, calculating the product of all numbers in a list:

Copy

The lambda function lambda x, y: x * y multiplies all the elements together.

6. Lambda with Default Arguments

Lambda functions can also have default arguments:

Copy

7. Lambda Inside Other Functions

You can also use lambda functions inside other functions or methods. For instance, defining a lambda function inside a higher-order function:

Copy

8. Lambda with max() and min()

Lambda functions are useful when you want to get the max or min element based on a custom key.

For example, getting the maximum length string from a list:

Copy

9. Nested Lambda Functions

You can nest lambda functions, though it’s generally discouraged for readability purposes.

For example:

Copy

In this example, the outer lambda function returns another lambda function that takes y and multiplies it by x.

10. Lambda for Mathematical Operations

Lambda functions can be used for mathematical operations directly.

Copy

Summary

  • Lambda functions are anonymous, one-line functions created with the lambda keyword.

  • They're often used in situations where you need a short, simple function for a brief period of time.

  • Common uses include filtering, mapping, reducing, sorting, and handling operations like applying custom functions to iterables.

Though lambda functions can make your code concise, they are best used for short, simple operations. For more complex logic, a regular function defined with def might be clearer.

Last updated