Python Lambda Function in Simple Words

Vlad Bashtannyk
Towards Dev
Published in
5 min readApr 19, 2022

--

In this article, you will find out more about the anonymous function, also known as lambda functions. You’ll learn what they are, their syntax, and how to use them (with examples).

Python Lambda Functions are anonymous function means that the function is unnamed. As we all know that the def keyword is used to define a normal function in Python. As well, the lambda keyword is used to define an anonymous function in Python.

Content Plan:

  1. How to Use Lambda Function
    1.1. Example of Lambda Function
  2. Difference Between Lambda and Def Function
  3. Lambda & Higher-Order Functions
    3.1. Example With filter()
    3.2. Example With map()
    3.3. Example With reduce()
  4. Lambda & List Comprehension
  5. Lambda & “if-else” Condition
  6. Lambda & Multiple Statements
  7. Conclusion

1. How to Use Lambda Function?

A lambda function in python has the following syntax.

lambda arguments: expression

Lambda functions can have any number of arguments, but only one expression. The expression is evaluated and returned. Lambda functions can be used wherever function objects are required.

1.1. Example of Lambda Function

Here is an example of a lambda function that doubles the input value.

Output:

10

In the above code, lambda x: x*2 is the lambda function. Here x is the argument and x*2 is the expression that gets evaluated and returned.

This function has no name. It returns a function object which is assigned to the identifier double. We can now call it a normal function. The statement:

double = lambda x: x*2

is equivalent to:

def double(x):
return x * 2
  • This function can have any number of arguments, but a single expression is evaluated and returned.
  • The lambda functions are available whenever function objects are required.
  • You need to keep in your knowledge that lambda functions are syntactically restricted to a single expression.
  • It has a variety of uses in particular fields of programming in addition to other types of expressions in functions.

2. Difference Between Lambda and Def Function

Let’s look at this example and try to understand the difference between a def defined function and lambda function. This is a code that returns the cube of a given value:

Output:

8
8

As shown in the example above both the defined_cube() function and the lambda_cube() function behaves in the same way and as expected. Let’s look a little deeper at the example above:

  • Without using Lambda: Here, both of them return the cube of a given number. But, while using def, we had to define a function with a name defined_cube() and pass it a value. After executing, we also needed to return the result from where the function was called using return keyword.
  • Using Lambda: Lambda definition does not include a return statement, it always contains an expression that is returned. We can also put a lambda definition anywhere a function is expected, and we don’t have to assign it to a variable at all. These are the simple lambda functions.

3. Lambda & Higher-Order Functions

We use lambda functions when we require a nameless function for a short period.

In Python, we generally use it as an argument to a higher-order function (a function that takes in other functions as arguments). Lambda functions are used along with built-in functions such as filter(), map() and reduce(), etc.

Let’s look at a few more common examples of lambda functions.

3.1. Example With filter()

The filter() function in Python takes in a function and a list as arguments.

The function is called with all the items in the list and a new list is returned which contains items for which the function evaluates to True.

Here is an example use of filter() function to filter out only even numbers from a list.

Output:

[4, 6, 10, 12, 14]

3.2. Example With map()

The map() function takes in a function and a list as arguments.

The function is called with all the elements of the list and a new list is returned which contains the elements returned by this function for each element.

Below is an example using of map() function to double all the elements in a list.

Output

[5, 15, 20, 30, 50, 55, 75, 60, 70]

3.3. Example With reduce()

The reduce() function takes in a function and a list as arguments. The function is called with a lambda function and an iterable and a new reduced result is returned. This performs a repetitive operation over the pairs of the iterable. The reduce() function is part of the functools module.

Output:

380

Here the results of the previous two elements are added to the following element and this goes on till the end of the list as:

5+15+20+30+50+55+75+60+70

4. Lambda & List Comprehension

In this example, we will use the lambda function with a list comprehension and lambda with for loop. We will print the table of 10 elements.

Output:

10
20
30
40
50
60
70
80
90
100

5. Lambda & “if-else” Condition

Let’s take a look at using if-else conditions in the lambda function. As you know python allows us to use one-line conditions and this is what we can put in a lambda function to handle the returned result.

For example, there are two figures and you have to determine a maximum number.

Output:

5

This method allows you to add conditions to a lambda function.

6. Lambda & Multiple Statements

Lambda functions don't allow multiple statements, however, we can create two lambda functions and then call the other lambda function as a parameter to the first function. Let’s try and find the second max element using lambda.

Output:

[9, 16, 30]

In the previous example, we have created a lambda function that sorts each sub-list of the given list. Then this list is passed as the parameter to the second lambda function which returns the n-2 element from the sorted list where n is the length of the sub-list.

Conclusion

Now you know how to use Python lambda functions and can:

  • Write and use lambda functions
  • Choose wisely between lambdas or defined Python functions
  • Use lambdas with higher-order functions or Python key functions
  • Use lambdas with a list comprehension
  • Add conditions to lambda functions

P.S. If you liked this article, follow me, click on “Claps” a few times and leave feedback. Good luck and efficient programming! Thank you!

LinkedInTwitter

--

--

Python developer with a passion for writing about IT and Python. 2 years of experience. Let's explore the world of programming together!