Python Tutorial (22) - lambda (anonymous function)

Time: Column:Python views:267

Python Lambda Functions

Python uses the lambda keyword to create anonymous functions.

A lambda function is a small, anonymous, inline function that can have any number of arguments but only one expression. Unlike a full function defined using the def keyword, a lambda function doesn't need a function name.

Lambda functions are typically used for writing simple, single-line functions, especially when functions are passed as arguments, such as in map(), filter(), and reduce().

Characteristics of Lambda Functions:

  • Anonymous: Lambda functions don't have a name and are used by assigning them to variables or passing them as arguments to other functions.

  • Single-line: Lambda functions usually consist of one line, making them ideal for writing simple functions.

Lambda Syntax:

lambda arguments: expression
  • lambda: A Python keyword used to define lambda functions.

  • arguments: The list of arguments that can have zero or more parameters, but must appear before the colon :.

  • expression: The single expression that evaluates and returns the result of the function.

Example 1: Lambda Function with No Parameters

f = lambda: "Hello, world!"
print(f())  # Output: Hello, world!

Output:

Hello, world!

Example 2: Lambda Function with One Parameter

Here’s an example where a lambda function accepts one argument a, adds 10 to it, and returns the result:

x = lambda a: a + 10
print(x(5))

Output:

15

Example 3: Lambda Function with Two Parameters

Lambda functions can take multiple parameters, separated by commas. Below is an example where two parameters a and b are multiplied:

x = lambda a, b: a * b
print(x(5, 6))

Output:

30

Example 4: Lambda Function with Three Parameters

Here’s an example where the lambda function takes three parameters and returns their sum:

x = lambda a, b, c: a + b + c
print(x(5, 6, 2))

Output:

13

Lambda Functions with Built-in Functions

Lambda functions are often used with built-in functions such as map(), filter(), and reduce() to perform operations on collections.

Example 5: Using Lambda with map()

In this example, map() applies a lambda function to square each element in the numbers list:

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

Output:

[1, 4, 9, 16, 25]

Example 6: Using Lambda with filter()

The filter() function, combined with a lambda, can be used to filter out even numbers from a list:

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

Output:

[2, 4, 6, 8]

Example 7: Using Lambda with reduce()

This example demonstrates how to calculate the cumulative product of a sequence using reduce() and a lambda expression:

from functools import reduce

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

# Use reduce() and lambda to calculate the product
product = reduce(lambda x, y: x * y, numbers)

print(product)  # Output: 120

Output:

120

In the above example, the reduce() function iterates over the numbers list, applying the lambda function to continuously update the cumulative product. The result is the product of 1 * 2 * 3 * 4 * 5, which equals 120.