Linux How To's | Bash Scripting | Python

Saturday, 17 December 2016

Python map() function - Many of times, we may require to execute same operation on different elements of a sequence. For this, we create an empty list, write a for loop, perform the operation and append result in the list. Python provides a built-in function - map(), to perform the similar operation. It receives at least two arguments - a function and one or more iterable objects, applies the function on each item of the iterables and returns a list with results of the operation. When we say, map() accepts function as an argument, it may be a built-in function or a normal function created using def construct or it can be a lambda. We will be learning how each of them can be used in the map function. But, I recommend you to have a read over our article on Python lambda function, if you haven't already.

Python map() function

As mentioned above, the map() function accepts a function (without parenthesis) and one or more iterables as arguments. So, we can write its syntax as below:

In above example, map function takes a function bin() (without parenthesis), which returns binary equivalent of a number, and a list myList. It applies bin() function on every list item and returns a list of result values. If this seems confusing, we take another example. Before that, we see what we would have done, if we weren't aware about map() function - a for loop may be.

Simple, isn't it? We just had to import math module, in order to use sqrt() function within a map() function. Now, we create a function using def and use it in map(). Our function cube() accepts an argument and returns it's cube i.e. it's 3rd power.

We can also provide multiple iterable objects to map() functions. In this case, items from all iterables are taken in parallel to create a sequence and the sequence is provided as arguments to the function. Seems confusing? Lets create three lists and let our function be a lambda that returns a tuple of the arguments provided. As a result, we get a list of tuples.

In this way, an item each from every list is taken out in parallel and the sequences L1[0], L2[0], L3[0], L1[1], L2[1], L3[1], etc. are provided as arguments to the function. If the lengths of the lists are unequal, None would be passed as an argument.

map() inside another map() - As we know, map() function accepts list as one of the arguments and returns a list. So, we can use a list returned by a map() function as an argument to another map() function, so that there will be nesting of two map() functions.

We now close our discussion on Python map() function. We have learned how a function, may it be built-in or created using def or lambda, can be applied on one or more iterable objects in an efficient way, thus avoiding use of for loops. In the next article, we learn about another important function used in functional programming - reduce() function. Please share your views and feedback in the comment section below and stay tuned. Thank you!