# Python Lambda Functions

In Python, a lambda function is a small anonymous function that can take any number of arguments, but can only have one expression. Lambda functions are defined using the `lambda` keyword, followed by the argument list and the expression to be evaluated.

Here’s an example of a lambda function that returns the square of a number:

```square = lambda x: x**2
```

This creates a lambda function called `square` that takes one argument `x` and returns `x**2`.

Lambda functions can also be used as arguments for other functions, such as `map()`, `filter()`, and `reduce()`. Here’s an example of using a lambda function with `map()`:

```numbers = [1, 2, 3, 4, 5]
squares = map(lambda x: x**2, numbers)
```

This creates a new list called `squares` that contains the squares of the numbers in the `numbers` list.

Lambda functions can also be used in conjunction with Python’s built-in sorting functions, such as `sorted()`:

```names = ['John', 'Sarah', 'Mike', 'Emily']
sorted_names = sorted(names, key=lambda x: len(x))
```

This sorts the `names` list by the length of each name, using a lambda function as the `key` argument.

Lambda functions are a useful tool in Python for creating small, one-time use functions that don’t require a full function definition.

### What’s the Distinction Between Lambda and Def Functions?:

The main distinction between lambda functions and def functions in Python is that lambda functions are anonymous functions that can only have one expression, whereas def functions are named functions that can have multiple statements.

Here are some other key differences between lambda functions and def functions:

• Syntax: Lambda functions are defined using the `lambda` keyword followed by the argument list and the expression to be evaluated, whereas def functions are defined using the `def` keyword followed by the function name, argument list, and function body enclosed in a block of code.
• Function name: Lambda functions are anonymous and do not have a name, whereas def functions have a name that can be used to call the function.
• Arguments: Lambda functions can take any number of arguments, but only one expression, whereas def functions can take any number of arguments and can have multiple statements.
• Return statement: Lambda functions automatically return the result of the expression they evaluate, whereas def functions require an explicit `return` statement to return a value.

Here’s an example of a lambda function and a def function that both calculate the square of a number:

```# Lambda function
square = lambda x: x**2

# Def function
def square(x):
return x**2
```

In this example, the lambda function and def function both take one argument `x` and return `x**2`. However, the lambda function is defined using a single expression, whereas the def function requires a `return` statement to return the result.

Overall, lambda functions are a useful tool for creating small, simple functions that do not require a full function definition, whereas def functions are better suited for more complex functions that require multiple statements and a named function definition.

### Using Lambda Function with filter():

In Python, `filter()` is a built-in function that takes an iterable object (such as a list, tuple, or set) and a function, and returns a new iterable object containing only the elements of the original iterable for which the function returns True.

You can use a lambda function as the function argument for `filter()`, which allows you to create simple filters without defining a separate named function.

Here’s an example of using a lambda function with `filter()` to filter a list of numbers:

```numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = filter(lambda x: x % 2 == 0, numbers)
```

In this example, the lambda function `lambda x: x % 2 == 0` is used as the function argument for `filter()`. This lambda function takes one argument `x` and returns True if `x` is even (i.e. if `x` modulo 2 is 0). The `filter()` function applies this lambda function to each element of the `numbers` list and returns a new iterable object containing only the even numbers.

You can also use a lambda function with `filter()` to filter a list of strings based on a condition. Here’s an example:

```fruits = ['apple', 'banana', 'cherry', 'orange', 'kiwi']
short_fruits = filter(lambda x: len(x) < 6, fruits)
```

In this example, the lambda function `lambda x: len(x) < 6` is used as the function argument for `filter()`. This lambda function takes one argument `x` and returns True if the length of `x` is less than 6. The `filter()` function applies this lambda function to each element of the `fruits` list and returns a new iterable object containing only the fruits with a length less than 6 (i.e. ‘apple’ and ‘kiwi’).

Using a lambda function with `filter()` can be a quick and easy way to filter iterables based on simple conditions without defining a separate named function.

### Using Lambda Function with map():

In Python, `map()` is a built-in function that takes an iterable object (such as a list, tuple, or set) and a function, and returns a new iterable object containing the result of applying the function to each element of the original iterable.

You can use a lambda function as the function argument for `map()`, which allows you to create simple functions without defining a separate named function.

Here’s an example of using a lambda function with `map()` to calculate the square of each number in a list:

```numbers = [1, 2, 3, 4, 5]
squares = map(lambda x: x**2, numbers)
```

In this example, the lambda function `lambda x: x**2` is used as the function argument for `map()`. This lambda function takes one argument `x` and returns the square of `x`. The `map()` function applies this lambda function to each element of the `numbers` list and returns a new iterable object containing the squares of the numbers.

You can also use a lambda function with `map()` to transform a list of strings. Here’s an example:

```fruits = ['apple', 'banana', 'cherry']
cap_fruits = map(lambda x: x.capitalize(), fruits)
```

In this example, the lambda function `lambda x: x.capitalize()` is used as the function argument for `map()`. This lambda function takes one argument `x` and returns a capitalized version of `x`. The `map()` function applies this lambda function to each element of the `fruits` list and returns a new iterable object containing the capitalized versions of the fruits.

Using a lambda function with `map()` can be a quick and easy way to apply simple functions to each element of an iterable without defining a separate named function.

### Using Lambda Function with List Comprehension:

In Python, you can use lambda functions with list comprehensions to create new lists based on existing lists. List comprehensions are a concise way of creating new lists by specifying a condition and an expression.

Here’s an example of using a lambda function with a list comprehension to filter a list of numbers:

```numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = [x for x in numbers if (lambda x: x % 2 == 0)(x)]
```

In this example, the lambda function `lambda x: x % 2 == 0` is used within the list comprehension to check if each element of `numbers` is even. The list comprehension filters the even numbers by iterating over the original `numbers` list, applying the lambda function to each element, and including only the elements for which the lambda function returns True.

You can also use a lambda function with a list comprehension to transform a list of strings. Here’s an example:

```fruits = ['apple', 'banana', 'cherry', 'orange']
cap_fruits = [((lambda x: x.capitalize())(fruit)) for fruit in fruits]
```

In this example, the lambda function `lambda x: x.capitalize()` is used within the list comprehension to capitalize each element of the `fruits` list. The list comprehension iterates over the original `fruits` list, applies the lambda function to each element, and includes the capitalized version of each element in the new `cap_fruits` list.

Using lambda functions with list comprehensions can be a concise way to create new lists based on existing lists. However, it’s important to keep in mind that lambda functions can make code less readable if they are too complex or if they are used excessively.

### Using Lambda with Multiple Statements:

In Python, a lambda function is typically used to define a simple, one-line function. Lambda functions are generally not used to define functions that have multiple statements, because the syntax of a lambda function does not allow for multiple lines of code.

However, you can use a lambda function to execute multiple statements by using semicolons to separate the statements. Here’s an example:

```my_func = lambda x: (x + 1; print("Result is", x))
my_func(2)
```

In this example, the lambda function `lambda x: (x + 1; print("Result is", x))` takes one argument `x`. The function adds 1 to `x`, and then prints the result along with a message. The semicolon is used to separate the two statements.

When you call the lambda function with an argument of `2`, it will print the message “Result is 2” and return a value of `3`.

While it is possible to use lambda functions with multiple statements, it is generally not recommended because it can make the code less readable and harder to maintain. If you need to define a more complex function, it’s usually better to use a regular `def` function instead of a lambda function.