Python lambda

In this article we study Python lambda function, i.e. anonymous functions.

In previous lessons we saw how to create functions using the keyword def followed by the function name and arguments if there are any.

On the contrary, the lambda function does not have a name, but only arguments and a single calculation expression.

The syntax of the lambda function in Python is therefore the following:

lambda argumentsexpression

Python lambda function – examples

Let’s take some examples to better understand how it works.

As a first example, let’s build a lambda function useful for calculating the cube of a number:


f_cube = lambda x : x * x * x
print(f_cube(3))

The same function using def could be written like this:



def f_cube(x):
      return x * x * x

print(f_cube(3))

The result is clearly the same.

We can also write very compactly like this:



print((lambda x: x * x * x)(3))

More arguments in Python lambda function

You can also enter multiple arguments, separating them by commas. Let’s take an example with a function that calculates the area of ​​a rectangle.



f_area = lambda x, y : x * y
print(f_area(4,5))

#in a compact way
print((lambda x, y: x*y)(4,5))

Evaluate a Boolean expression

We can also evaluate a Boolean expression.

For example, let’s take a number into consideration and decide whether it is even or odd.



f_even = lambda x : x % 2 == 0
print(f_even(8))
print(f_even(9))

The output produced will therefore be this:

True
False

Why use the Python lambda function

Surely one of the reasons why it is used is to save lines of code, improving its readability. In fact, when I have to use a function for a short time, there is no need to define it.

Also one of the major fields of use is when you want to pass a function as an argument to another function.

We will very often see lambda functions used with Python map () or filter () functions.

Let’s take a first example using filter ():


list_number = [11,8,4,5]

new_list = list(filter(lambda x: x % 2 == 0, list_number))

print(new_list)

In this case the output produced will be this: [8, 4]

Or we can also use map ().


list_number = [11,8,4,5]

new_list = list(map(lambda x: x % 2==0, list_number))

print(new_list)

The output produced will be this: [False, True, True, False]

The list will be populated with Boolean values.

We could also use the for loop in:


list_number = [11,8,4,5]

new_list = [even for even in list_number if even %2 == 0]

print(new_list)

Example with reduce():


from functools import reduce

list_number = [11,8,4,5]

sum_numbers = reduce (lambda x, y: x + y, list_number)

print(sum_numbers)

Finally we can add that the use of the lambda function in Python is particularly suitable when working with some libraries, such as Pandas, Numpy, etc.

Instead, it cannot be used when, for example, we want to perform multiple independent operations within a function. Or when I need to write multi-line instructions.

Some useful links

Python tutorial

Python Compiler

Install Python

Variables

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *