Python map() Function

This article covers the use the Python map Function,

The map() function is used apply a specified function to a given iterable and return the result in the form of an iterable object (lists, tuples, sets). The syntax of the map() function is as shown below.

map(function, iterable, ...)

There may be more than iterable passed into the map() function, hence the dots. However, the more iterables, the more parameters the given function must have, as the iterables are used as input arguments for the function. In the case of iterables of different sizes, The map function will stop by itself once the shorter iterables is finished.

Creating a function for the Map object

First we’ll create a simple function which we can use with our map() object.

def inc(x):
    x = x + 1
    return x

The above code will increment any number passed to it and return the result. Remember to return the result, else the map object will never receive the value of x.

Creating the map function

Next up is creating an iterable and passing it into the map function along with the function we created above.

lis = [1,2,3,4,5]
result = map(inc,lis)

Iterating through the map object

An iterable object is now returned to us, which we can iterate over using the below for loop. From the output, you can see that every number is now incremented by one.

for x in result:

Map function with multiple arguments

As mentioned above, you can pass multiple iterables into the map() function. Keep in mind however, the number of parameters on the given function and the number of iterables must be the same.

Creating a Function

Here we create an appropriate function. The function below has two parameters x and y, which is proceeds to add together and return.

def add(x,y):
    result= x + y
    return result

Creating a map function

Here we create two unique lists and pass them into the map function. Make sure the order of your iterables matches the order of the parameters you declared in the function.

lis = [1,3,5,8,0,4]
lis2 = [4,7,3,4,1]
result = map(add,lis, lis2)

Iterating through the returned iterable

Finally we iterate through the result, and see that both lists were added successfully.

for x in result:

However only 5 results were returned as the shortest iterable has only 5 elements.


As a side note, the above function would work even if the two iterables were of a different type. Say, one was a list and the other was a python tuple, the map function would still work just fine.

Map object

This is a small segment on the map object itself that gets returned from the map function. We’ll be borrowing some code from the first example.

def inc(x): 
      x = x + 1
      return x

lis = [1,2,3,4,5]
result = map(inc,lis)

Attempting to find out it’s type will return the following.

<class 'map'>

What if we wish to obtain the result of the map object in the form of a list or tuple? Even though the map object is an iterable, it does not show us the data in such a manner. See below.

<map object at 0x000001B0ECFABD88>

Luckily we can use the list, tuple and set built in functions to get around this issue. Whichever you desire, you can use.

def inc(x): 
      x = x + 1
      return x

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

result = map(inc,lis)

result = map(inc,lis)

result = map(inc,lis)
(2, 3, 4, 5, 6)
[2, 3, 4, 5, 6]
{2, 3, 4, 5, 6}

This marks the end of the Python map function article. Any suggestions or contributions for CodersLegacy are more than welcome. Questions can be asked in the comments section.

Notify of
Inline Feedbacks
View all comments