The Startup
Published in

The Startup

Lambda and Map() Functions in Python

Lambda and map() functions in Python

This is a tutorial on lambda and map() functions in Python. Here, we will consider Python3. We start with the lambda function, and then we discuss the map() function in Python. This tutorial will help to understand the concepts of lambda and map() functions in Python.

Lambda function

At first, we will learn about the basics of the lambda function in Python. A lambda function is a simple one-line function. It doesn’t use def or return keywords. These are implicit here.

Syntax:

lambda args : exp

where args represents arguments and exp is the expression. The expression is executed and the result is returned. Note that lambda functions can have any number of arguments but only one expression.

Now, we will understand more about the Python lambda function using a few examples.

Example 1:

Let us define a lambda function that doubles the given value.

db = lambda x : 2*x

Here, exp is 2*x and args is x. Now, we call this function as follows:

db(3)

This will produce 6 as the output.

Example 2:

Let us consider another example of a lambda function to compute the sum of two numbers.

add = lambda x,y : x+y

Note that two arguments are used in this example. Now, we call this function with the parameters 3.5 and 6 as follows:

add(3.5,6)

This will result in 9.5.

Example 3:

The following lambda function determines the maximum between two given numbers.

max = lambda x,y : x if x>y else y

Notice that a conditional statement is used here in the expression of the lambda function. Now, we call this function with two parameters 9 and 15 as follows:

max(9, 15)

This will return 15 as the output.

This tutorial on the Python lambda function was initially published in my blog.

Python map() Function

Now, we will learn about the map() function in Python. When we want to apply the same function to each element of a sequence, we use the map() function. This will return the modified sequence.

Syntax

map(func, iter)

where func is the function to execute for each item, and iter is a sequence, collection, or an iterator object.

Example 4:

Let us consider an example where Python’s map() function is used. In this example, a lambda function is created, at first, to get the square of a given number. In this example, list1 is a Python list. Finally, a new Python list, list2 is created using the lambda function.

list1 = [1,2,3,4,5]list2 = list(map(lambda x : x**2, list1))

The content of list2 will be [1, 4, 9, 16, 25].

Remember that you can write your own function definition to square the numbers in a list. Consider the following function square():

def square(list1):
list2 = []
for x in list1:
list2.append(x**2)
return list2

Now, we call the function square() with the argument list1 as follows:

list2 = square(list1)

This will also create the same list [1, 4, 9, 16, 25].

The above example shows how to square the elements of a list in Python using a user-defined function.

We will now learn how to use a user-defined function within Python’s map() function. In our earlier example, we have used a lambda function to get the square of an element. Now, in the following example, we use a user-defined function sqr() with Python’s map() function as follows:

def sqr(n):
return n**2

Now, we use the Python map() function to create list2 from list1, as follows:

list2 = list(map(sqr, list1))

Here, we will get the same list [1, 4, 9, 16, 25].

In this example, we have created list2 from list1 using three different ways. At first, we have created list2 using a lambda function. Then we have generated list2 using a normal function call. At last, we have created list2 from list1 using the Python map() function.

This tutorial on the Python map() function was initially published in my blog here.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dr. Soumen Atta, Ph.D.

Dr. Soumen Atta, Ph.D.

Postdoctoral Researcher at Laboratoire des Sciences du Numérique de Nantes (LS2N), Université de Nantes, IMT Atlantique, Nantes, France.