Skip to content

Latest commit

 

History

History
127 lines (81 loc) · 3.57 KB

lambda.md

File metadata and controls

127 lines (81 loc) · 3.57 KB

Definition

A lambda function is a small anonymous function in Python. "Anonymous" means a function that is without a name. While normal functions are defined using the def keyword, in Python anonymous functions are defined using the lambda keyword.

Syntax

The syntax of a lambda function is quite simple:

lambda arguments: expression
  • lambda: A keyword that signifies that this is a lambda function.
  • arguments: (Optional) The parameters or inputs to the function.
  • expression: A single expression that the function returns.

Characteristics

  • Anonymity: Lambda functions are anonymous, meaning they are not declared with the standard def keyword.
  • Simplicity: They can have any number of parameters but can only have one expression.
  • Usage: They are used for short-duration, non-reusable operations.
  • Return: The expression value is returned on execution.

Basic Example

Here's a basic example to illustrate a lambda function:

# Lambda function that adds 10 to the number passed in as an argument
f = lambda x: x + 10
print(f(5))  # Output: 15

Explanation and Use-cases

  1. Single-use or temporary functions: When you need a function for a short period of time and do not want to formally define it.

  2. Functional arguments: Passing a function as an argument to higher-order functions, such as map(), filter(), and reduce().

Extended Examples

Example 1: Using lambda with map()

map() applies a given function to all the items in an input list (or any other iterable).

my_list = [1, 2, 3, 4, 5]
squared_list = list(map(lambda x: x**2, my_list))

Example 2: Using lambda with filter()

filter() creates a list of elements for which a function returns True.

my_list = [1, 2, 3, 4, 5]
even_list = list(filter(lambda x: x % 2 == 0, my_list))

Example 3: Using lambda with reduce()

reduce() applies a rolling computation to sequential pairs of values in a list.

from functools import reduce

my_list = [1, 2, 3, 4, 5]
product = reduce(lambda x, y: x * y, my_list)

Let's check the output of the extended examples by running the code snippets.

from functools import reduce

# Example 1: Using lambda with map()
my_list = [1, 2, 3, 4, 5]
squared_list = list(map(lambda x: x**2, my_list))

# Example 2: Using lambda with filter()
even_list = list(filter(lambda x: x % 2 == 0, my_list))

# Example 3: Using lambda with reduce()
product = reduce(lambda x, y: x * y, my_list)

(squared_list, even_list, product)

# RESULT
([1, 4, 9, 16, 25], [2, 4], 120)

Example 1: Using lambda with map()

my_list = [1, 2, 3, 4, 5]
squared_list = list(map(lambda x: x**2, my_list))

# Output: 1,4,9,16,251,4,9,16,25

Each element of my_list is squared.

Example 2: Using lambda with filter()

my_list = [1, 2, 3, 4, 5]
even_list = list(filter(lambda x: x % 2 == 0, my_list))

# Output: 2,42,4

Only the even numbers are retained from my_list.

Example 3: Using lambda with reduce()

my_list = [1, 2, 3, 4, 5]
product = reduce(lambda x, y: x * y, my_list)

# Output: 120

The product of all elements in my_list is calculated as (1×2×3×4×5=120)(1×2×3×4×5=120).

Lambda functions can be very useful for performing quick calculations without defining a full function. However, they should be used judiciously to ensure that code remains readable. If a computation is complex or requires documentation, it's often better to define a traditional function using def.