What is a lambda function in Python and how to use it?

Table of Contants

Introduction:

In Python, a lambda function is a small, anonymous function defined with the lambda keyword. Unlike regular functions created with def, lambda functions are designed for situations where you need a simple function for a short period of time and don’t need to give it a name. Lambda functions can be useful for quick, one-off operations and are commonly used in functional programming with functions like map(), filter(), and sorted().

Syntax of Lambda Functions

The syntax for a lambda function is:

  • lambda: The keyword used to define a lambda function.
  • arguments: The parameters the function accepts.
  • expression: A single expression that the function evaluates and returns.

Example of a Basic Lambda Function

  • Here, lambda x, y: x + y creates an anonymous function that takes two arguments and returns their sum. The function is assigned to the variable add.

Using Lambda Functions with Built-in Functions

Lambda functions are often used with Python's built-in functions that accept other functions as arguments.

Example : Using map()

The map() function applies a function to all items in an iterable (e.g., a list).

  • lambda x: x ** 2 creates a function that squares its input, and map() applies this function to each element in the numbers list.

Example : Using filter()

The filter() function filters elements from an iterable based on a function that returns True or False.

  • lambda x: x % 2 == 0 creates a function that returns True for even numbers. filter() uses this function to filter out odd numbers from the numbers list.

Example : Using sorted()

The sorted() function can take a key argument to specify a function to determine the sort order.

  • lambda p: p[1] creates a function that returns the second element of a tuple. sorted() uses this function to sort the list of tuples by the second element.

Characteristics of Lambda Functions

  1. Anonymous: Lambda functions are unnamed and defined in a single line. They are often used where a small function is needed temporarily.
  2. Single Expression: They can only contain a single expression, which is evaluated and returned. They cannot contain multiple expressions or statements.
  3. Return Value: The result of the expression is automatically returned. You don’t need to use the return keyword.

When to Use Lambda Functions

  • Short-term Use: When a function is needed temporarily and only used in one place.
  • Functional Programming: In combination with functions like map(), filter(), and sorted().
  • Conciseness: For simplifying code where defining a full function with def might be unnecessarily verbose.

Conclusion:

Lambda functions in Python provide a concise way to create anonymous functions for short-term use. Their single-expression limitation makes them ideal for simple operations and functional programming scenarios. By understanding and utilizing lambda functions effectively, you can write more concise and readable Python code, especially when working with functions that require callable objects.

Similar Questions