In this article we will explore how to use the Python map() function.

Table of Contents



Introduction

In Python, when we work with multiple iterables (lists, tuples, or strings), we often need to apply a function to every element in an iterable.

Python map() function is a built-in function that allows to “map” or apply a particular function on every element in an iterable and returns a new iterable with modified elements.

The map() function process is defined as:

map(function, iterable) -> map object

where further the map object can be converted to an iterable (for example list using list() constructor).


Map a function over a list using map()

Let’s get started with a simple example of apply a function to a list using map() function.

We will define a square function that will take a number as an argument and return its squared value, and then apply this function on a list of numbers to get a list of their squared values.


First, we will create a list of numbers:


#Create a list of numbers
nums = [1, 2, 3, 4, 5]

Next, we will define a square function and test it:


#Define a function to square values
def square(x):
    return x*x

#Test function
squared_val = square(3)
print(squared_val)

and you should get:

9

Now that the function is working, we can apply it on the nums list.



#Square values in nums list
squared_vals = map(square, nums)

#Convert map object to a list
squared_vals = list(squared_vals)

#Print squared values
print(squared_vals)

and you should get:

[1, 4, 9, 16, 25]

As you can see, we’ve been able to quickly apply the square function on the nums list to get the squared values.


Map a function over multiple lists using map()

In the previous section we had a square function that takes one argument and applied it over one nums list.

In this section we will create a function that takes two arguments and will apply it over two lists using map() function.

First, we will create two lists of numbers:


#Create two lists of numbers
nums1 = [1, 3, 5, 7, 9]
nums2 = [2, 4, 6, 8, 10]

Next, we will define an add function and test it:


#Define a function to add values
def add(x, y):
    return x+y

#Test function
add_val = add(3, 5)
print(add_val)

and you should get:

8

Now that the function is working, we can apply it on the nums1 and nums2 lists.



#Add values from two lists
add_vals = map(add, nums1, nums2)

#Convert map object to a list
add_vals = list(add_vals)

#Print added values
print(add_vals)

and you should get:

[3, 7, 11, 15, 19]

which is a single list of values after addition numbers from lists nums1 and nums2.


Map a lambda function over multiple lists using map()

In the previous section we created an add function that takes two arguments and then can be applied on two lists.

But can we do the same operation without defining a function with multiple arguments?

We can do it using a Python lambda function together with map() function.

Let’s reuse the lists of numbers from the previous section:


#Create two lists of numbers
nums1 = [1, 3, 5, 7, 9]
nums2 = [2, 4, 6, 8, 10]

Now we can apply a lambda function with addition on the nums1 and nums2 lists.


#Add values from two lists
add_vals = map(lambda x, y: x + y, nums1, nums2)

#Convert map object to a list
add_vals = list(add_vals)

#Print added values
print(add_vals)

and you should get:

[3, 7, 11, 15, 19]

which is the same result as in the previous section.


Conclusion

In this article we explored how to use the Python map() function.

Now that you know the basic functionality, you can practice using it with other iterable data structures for more complex use cases.

Feel free to leave comments below if you have any questions or have suggestions for some edits and check out more of my Python Functions tutorials.