Grammar/Environments

[Python] How to Use Lambda Function

This article shows how to use lambda function.

Contents

  • What is lambda function ?
  • Basic usage of lambda function.
  • How to use if statements in lambda function
  • Benefits and example code of lambda function

sponsored link

What is lambda function ?

A lambda function in Python is a small anonymous function.

When defining a function in Python, "def statement" is used to give a "name" to the function.

But, by using lambda function, you can create a function without a "name".

Difference between lambda function and def statement

The difference between lambda function and def statement is explained below.

Usually, functions are defined using def statement.

def "function name" :
return

On the other hand, functions are defined using lambda function.

lambda "argument" : "return value"

In the next section, I show the usage of lambda function in detail with example code.

sponsored link

Basic usage of lambda function.

I show basic usage of lambda function.

The same process is described with a def statement and a lambda function.

Example of defining function with def statement.

def squared(x):
    calc = x*x
    return calc

eq1 = squared(2)
print(eq1)
# 4

Example of defining function with lambda function.

eq2 = lambda x : x*x
print(eq2(2))
# 4

You will notice that the description using lambda is more concise.

Argument of lambda function.
--- Two or more arguments / default argument ---

Example of two or more arguments.

eq3 = lambda x, y=1 : x*x + y*y
print(eq3(2, 3))
# 13

Example of default argument.

eq3 = lambda x, y=1 : x*x + y*y
print(eq3(2))
# 5

Specify y=1 as the default argument.

sponsored link

How to use if statements in lambda function

In lambda function, "if statements" can also be used.

lambda "argument" : "return value for true" if "conditional statement" else "return value for false"

Example of combination of lambda function and if statement.

is_zero = lambda value : "zero" if value == 0 else value

print(is_zero(0))
# zero
print(is_zero(5))
# 5

sponsored link

Benefits and example code of lambda function

A lambda function is useful for the following cases.

・Formulas can be written on a single line.
・Created function are not used frequently.

In the case of lambda, the function definition is on the same line.

This save the user trouble of looking up the function.

This is a benefit that leads to code readability.

The example code makes it easy to understand the readability of lambda function.

I show example code using map and sorted functions.

Combination of "map()" and lambda function

By using map(), function can be applied to all elements of the iterable object( list, tuple, etc.).

Here is an example code.

Each element of the list is multiplied by "2".

num_list = [1, 2, 3, 4, 5]

map_double = map(lambda x: x*2, num_list)
print(map_double)
# <map object at 0x0000013E9A843D68>
print(list(map_double))
# [2, 4, 6, 8, 10]

Please refer to the following article for more information on "map()".

[Python] How to use map() Function

Combination of "sorted()" and lambda function

By using sorted(), the elements of the list are sorted.

Here is an example code.

The second element in each list is used to sort the list.

num_list = [[1, 10, 4], [2, 5, 8], [3, 2, 12]]

new_num_list = sorted(num_list, key=lambda x: x[1])
print(new_num_list)
# [[3, 2, 12], [2, 5, 8], [1, 10, 4]]

Please refer to the following article for more information on "sorted()".

[Python] How to Sort List

sponsored link

-Grammar/Environments
-