Multiline Lambda Function in Python

Multiline Lambda Function in Python

  1. Lambda Function in Python
  2. Add Multiple Lines in Lambda Functions in Python
  3. Alternative to Multiline Function in Python
  4. Benefits of Python Lambda Functions

This short article will first explain Lambda functions and the best practices to write them. Then, we will explain if the multi-line Lambda function can be written in Python or not.

Lambda Function in Python

The lambda functions are anonymous as they don’t require any name. Like any normal function in Python, a Lambda function may have multiple arguments but only one expression.

The lambda function is generally used when we want to perform a small or one-line operation.

You may write short and one-time use functions using lambdas, which can help you save time and space in your code. Hence, improve the readability of your code.

The Lambdas are particularly helpful when calling functions like filter() that need a function as a parameter for a callback.

Lambda Function Syntax

Lambda function’s syntax is as follows:

lambda [arguments] : expression

The keyword lambda is used to start the lambda function. After that, the optional arguments are provided, and the given expression is applied to the arguments.

The lambda functions don’t explicitly return any value but only the function object.

In the below code, we defined a lambda function with the argument a and the expression a + 10. This function takes a as an argument and adds 10 to it.

x = lambda a : a + 10

Since the above function does not return any value, we need to call the object like this:

>> x(5)

We passed 5 as an argument value with object x, which returned 15.

Similarly, a Lambda function with multiple arguments can be defined as:

Value1= 35
Value2= 4
Find_remainder = lambda x , y : x % y
print(Find_remainder(Value1, Value2))

The above code returns the following output.


Add Multiple Lines in Lambda Functions in Python

“Guido van Rossum (Python creator) answers this question in one of his blog posts. He admits that it’s theoretically possible, but the solution is not a Pythonic way to do that.”

In Python, if the expression is very long, you can use the \ backslash character to write the remaining code on the new line.

fn = lambda x: 1 if x \
                     else 2

The above code returns the following output.


Alternative to Multiline Function in Python

If someone needs to use multiple operations in Python, they can always define a regular function. In Python, a regular function can be defined as:

def Func_Identifier( [list of arguments] ):
    [Function Body Block ]
    return [Val or identifier]

Benefits of Python Lambda Functions

There are few clear benefits of the lambda function, but we will briefly discuss a few.

Lambda With the filter() Function

You may quickly filter through a list using a lambda function in the filter() method. Your lambda function would provide a Boolean result that indicates whether or not the item should be maintained in this application.

The filter() method returns a subset of list items for which the lambda expression evaluates to true.

For example, the filter() in the code below returns a subset of the raw_data list with all items greater than 55.

raw_data = [3, 4, 6, 7, 11, 14, 19, 25, 45, 49, 53, 68, 72]
filtered_data = list(filter(lambda val: (val > 55), raw_data))


[68, 72]

Lambda With the map() Function

While using the map() function, you apply expressions or operators to each item in a given sequence or list through the map() method.

For example, we may use map() and a lambda function to multiply each integer in a list by 8. This specific example would be coded as:

list_of_numbers = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
modified_list = list(map(lambda val: val * 8, list_of_numbers))


[16, 32, 48, 64, 80, 96, 112, 128, 144, 160]

A more comprehensive note on lambda expressions can be found here.

Related Article - Python Lambda

  • Python Lambda Closure
  • Use Await in a Python Lambda
  • Lambda Function in Multiple Lines in Python
  • Why There Can't Be a Multiline Lambda in Python
  • The filter() Method and Lambda Functions in Python