All You Need to Know About the Lambda Functions in Python
By: Erdem Isbilen
Tags: Python, Python3, Lambda, Data Science, Programming

All You Need to Know About the Lambda Functions in Python

Practical examples and real usage of lambda functions in Python

Photo by Clint Patterson on Unsplash

If you’ve heard of lambda functions in Python but not sure about how to use them, you’re in the right place.

This article will provide all you need to know about the lambda functions in Python:

What are lambda functions in Python, how they differ from the normal functions?

Why lambda functions are useful and when to use them?

Practical examples and real usage of lambda functions in Python

What are lambda functions in Python, how they differ from the conventional functions?

Lambda functions are defined without a name so they are also called anonymous or nameless functions. We do not have to assign a name to lambda function as we do when defining normal functions.

# lambda function syntax

# lambda arguments: expression

lambda x,y: x+y # conventional function definition

def add_x_y(x,y):

return (x+y)

In addition, with lambda functions, we can have any number of arguments but we can only define a single line expression. It is not possible to define multi-line expressions as we can do when creating normal functions.

Why lambda functions are useful and when to use them?

When you need a single expression function which will be used once in your code, there is no need to define a normal function. You can define a lambda function whenever and wherever it is needed.

Lambda functions are also used when a function is needed as an argument to another function. Instead of defining a normal function, using a lambda function is more convenient and simple for such cases.

Practical examples and real usage of lambda functions in Python

You can find below several examples of how lambda functions are used.

# use of lambda functions

# x is the argument

# x * 2 is the expression which will be returned by lambda function double_x = lambda x: x * 2

print(double_x(4))

# output 8

# use of lambda functions

# x and y are the arguments

# x * y is the expression which will be returned by lambda function mult_x_y = lambda x,y: x * y

print( mult_x_y (4,5))

# output 20

Lambda functions are generally used in higher-order functions (filter(), map(), reduce() are very good examples of such functions) which take in another function as an argument. Let’s see how lambda function is used in such cases.

Example use with filter()

Filter() function takes a function and a list as arguments, then it returns the list items for which the function evaluates True.

# Use of lambda functions with filter() function

# Program to filter out only the items which are greater than 5 my_list = [1, 2, 3, 4, 5, 6, 7, 8]

new_list = list(filter(lambda x: x > 5 , my_list))

print(new_list) # output

[6, 7, 8]

In the example above, the filter function gets my_list and lambda function as arguments. It evaluates every list item with the expression ( x > 5) provided in the lambda function. If the evaluation result is True, then the new list contains this specific element. If not, the function filters out the element and exclude it from the new list.

Instead of the lambda function, we could be providing a normal function as an argument. But this would increase the complexity of the code. Whenever it is possible, using a lambda function makes the code simpler.

Example use with map()

Similarly, the map() function takes a function and a list as arguments. The function provided as an argument is called for each list item and returned results are contained in a new list.

# Use of lambda functions with map() function

my_list = [1, 2, 3, 4, 5, 6, 7, 8]

new_list = list(map(lambda x: x * 5 , my_list))

print(new_list) # output

[5, 10, 15, 20, 25, 30, 35, 40]

Example use with reduce()

reduce() function also takes a function and a list as arguments. This time, instead of returning a list, it returns a single value by applying the lambda function cumulatively to all the items in the list provided.

my_list = [1, 2, 3, 4, 5, 6, 7, 8]

result = reduce(lambda x,y: x + y , my_list)

print(result) # output

36 my_list = [1, 2, 3, 4, 5, 6, 7, 8]

result = reduce(lambda x,y: x * y , my_list)

print(result) # output

40320 my_list = [1, 2, 3, 4, 5, 6, 7, 8]

result = reduce(lambda x,y: x if x > y else y , my_list)

print(result) # output

8

Summary and Key Takeaways

In this short article, I have explained what lambda functions are and how to use them in Python. The key takeaways are;

Lambda functions are defined without a name so they are also called anonymous or nameless functions .

. We can have any number of arguments in lambda functions but we can only define a single line expression .

in lambda functions but we can only define a . You can use lambda function when you need a single expression function which will be used once in your code.

when you need a which will be used once in your code. Lambda functions are also used when a function is needed as an argument to another function.

I hope you have found the article useful and you will start using lambda functions in your own code.