Python is a powerful and easy-to-learn programming language. One of its interesting features is the lambda function. In this guide, we will learn what lambda functions are, how to use them, and see some practical examples to understand them better.

What is a Lambda Function?

A lambda function in Python is a small, anonymous function. It is called “anonymous” because it does not have a name. It is also known as a “lambda expression”. Lambda functions are used for short, simple tasks, and are defined using the keyword lambda.

Syntax of Lambda Functions

The syntax of a lambda function is quite simple:


lambda arguments: expression

  • lambda is the keyword.
  • arguments are the inputs to the function.
  • expression is the single expression that the lambda function evaluates and returns.

Example 1: Basic Lambda Function

Let’s start with a simple example:


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

In this example:

  • lambda x, y: x y creates a lambda function that takes two arguments x and y and returns their sum.
  • add is a variable that holds the lambda function.
  • add(2, 3) calls the lambda function with x=2 and y=3, and it returns 5.

Example 2: Lambda Function in map()

Lambda functions are often used with functions like map(), filter(), and reduce().

Using map()


The map() function applies a given function to all items in a list (or any other iterable) and returns a new list with the results.


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

In this example:

  • lambda x: x**2 is a lambda function that squares a number.
  • map(lambda x: x**2, numbers) applies this lambda function to each item in the numbers list.
  • list() converts the result to a list.

Example 3: Lambda Function in filter()

The filter() function filters items out of a list (or any other iterable) that do not match a condition.


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

In this example:

  • lambda x: x % 2 == 0 is a lambda function that returns True if x is even.
  • filter(lambda x: x % 2 == 0, numbers) applies this lambda function to each item in the numbers list and keeps only the even numbers.
  • list() converts the result to a list.

Example 4: Lambda Function in reduce()

The reduce() function applies a function of two arguments cumulatively to the items of a list (or any other iterable), from left to right, so as to reduce the list to a single value.


from functools import reduce

numbers = [1, 2, 3, 4, 5]
product = reduce(lambda x, y: x * y, numbers)
print(product)  # Output: 120

In this example:

  • lambda x, y: x * y is a lambda function that multiplies two numbers.
  • reduce(lambda x, y: x * y, numbers) applies this lambda function cumulatively to the numbers list to get the product of all numbers.

When to Use Lambda Functions

Lambda functions are useful when you need a simple function for a short period of time and you don’t want to define a full function using def. They are great for quick, throwaway operations, especially as arguments to higher-order functions like map(), filter(), and reduce().

Conclusion

Lambda functions are a powerful feature in Python that allow you to create small, anonymous functions quickly and easily. They are especially useful when used with functions like map(), filter(), and reduce(). By understanding and practicing with lambda functions, you can write more concise and readable Python code.

I hope this guide helps you get started with lambda functions in Python. Happy coding!