Lambda Function in Python

Video Tutorial
FREE
Lambda Function thumbnail
This video belongs to
Python Course for Beginners With Certification: Mastering the Essentials
16 modules
Certificate
Topics Covered

What is the Python Lambda Function

In Python, a Lambda function is a small anonymous function defined using the lambda keyword. Unlike regular functions declared with the def keyword, lambda functions can have many number of arguments but only single expression. It is evaluated and returned when the lambda function is invoked. Lambda functions are handy for short, throwaway tasks that are not meant to be reused elsewhere in your code.

Python's lambda function is a convenient, yet powerful tool for creating small, anonymous functions for short-term use, ideal for simplifying code in functional programming constructs.

Syntax of Python Lambda

A lambda function can take any number of arguments including no argument, but they contain only one expression. The expression is evaluated, and the lambda function automatically returns the resulting value.

Syntax of Python Lambda

Takeaways:

  • Lambda functions are small, anonymous functions defined using the lambda keyword.
  • Lambda function can take any number of arguments but can return a single expression.

Lambda Function in Python Use Cases

Lambda functions in Python are versatile and can be used in various scenarios. Here are some specific use cases with examples and their outputs:

Condition Checking

Lambda functions can be used for quick, inline condition checking.

Example:

Output:

Python Lambda Function with List Comprehension

They can be combined with list comprehension to apply a function to every element in a list.

Example:

Output:

Python Lambda Function with If-Else

Lambda functions can also incorporate simple if-else conditions within them.

Example:

Output:

Python Lambda Function with Multiple Statements

While lambda functions are limited to a single expression, you can simulate multiple operations with logical operators or conditional expressions.

Example:

Output:

In this example, x is first incremented by 2, then its square is returned. The assignment expression (:=, introduced in Python 3.8) allows us to update the variable x within the lambda function.

Important Note: Lambda functions are meant for simple, concise operations. While it's possible to stretch their use, as shown in example d), it's generally better practice to use regular functions for more complex logic to maintain code readability and clarity.

How to Use Lambda Function with filter()

The filter() function in Python takes in a function and a list as arguments. That function is called with every item in the list, and a new list is returned that contains items for which the function evaluates to True.

Use of Lambda Function in Python with filter

The following example uses the filter function to filter out numbers greater than 10:

Output:

Explanation:

We created a list with some numbers and then used the filter function to filter out numbers greater than 10 by providing a lambda function that checks for the same: lambda x: x > 10. As the filter returns a filter object rather than a list, we must convert its result to a list. Then, we finally printed the result.

Using Lambda Function in Python with map()

The map() function in Python also takes in a function and a list as arguments. That function is called for all the items in that list, and the latest list is returned, that contains items returned by that function.

Use of Lambda Function in Python with map

The following example uses the map function to double all the numbers in a list:

Output:

Explanation: We created a list with some numbers and then used the map function to double the numbers by providing the following lambda function: lambda x: x * 2. As the map returns a map object rather than a list, we must convert its result to a list. Then, we finally printed the result.

Using Lambda Function in Python with reduce()

The reduce() function in Python, which is part of the functools module, is a powerful tool often used with lambda functions. It applies a function of two arguments cumulatively to the items of an iterable, reducing the iterable to a single value. When used with lambda functions, reduce() becomes even more versatile.

Here's how you can use a lambda function with reduce():

Example 1: Sum of a List

Explanation: The lambda function takes two arguments, x and y and adds them. reduce() applies this lambda function to the starting two elements of the list, then to the result and the next element, and so on, effectively calculating the sum of all elements in the list.

Example 2: Finding Maximum in a List

Explanation: The lambda function compares two values, x and y, and returns the greater one. reduce() applies this function cumulatively to the list items, resulting in finding the maximum value in the list.

Example 3: Concatenating Strings

Explanation: Here, the lambda function concatenates two strings with a space in between. reduce() applies this function across the list of strings, effectively concatenating all the strings into a single sentence.

Conclusion

  • Lambda in Python are small, anonymous functions defined using the lambda keyword.
  • A Python lambda function can take any number of arguments, but they contain only one expression.
  • Python lambda function automatically returns the evaluated value of the expression.
  • Lambda functions are mainly used with higher-order functions.
  • Lambda functions help to make code shorter but can lead to several issues in development:
    • Imprecise tracebacks.
    • Inability to include statements like raise in the function body.
    • No type annotation support.
    • Poor readability.

Explore Scaler Topics Python Tutorial and enhance your Python skills with Reading Tracks and Challenges.