What is Python map function?

The map() function is used to apply a function (custom or built-in) to each item of one or more iterables (lists, tuples, set etc.). The map function returns iterable e.g. a list after applying the given function – yielding the result.

Map with list example map with lambda example

To make it clearer, consider this example. Suppose, we have a list of mixed items i.e. strings, integers, and float.

lst_mixed = [‘The’,’map’,’function’,9.9, 50]

We want to convert that list into the string. The join() method can be used for converting the list into a string, however, the list must contain string items only. Otherwise, it will produce an error.

So, how can we convert that list into the string?

We know that the str() function can be used for converting the integers and floats to strings. So, if we run the str() function on list items, the int and float will be converted to list and then we can use the join function for converting that list into strings.

Now, next question is, how to run the str() function on each item in a list? Read again the definition of map function!

So, by giving the str() function to the map() function and providing the list (an iterable), this is how the result can be achieved:

str_join_map = ” “.join(map(str, lst_mixed))

The complete code and output will be:

Python map

How to use the map function?

The general syntax of using the Python map function is:

map(function, iterable, …)

A few points about the map function

  • The map function takes two parameters: a function and iterable (tuples, list etc.)
  • The function is applied to each item of the iterable.
  • You may provide one or more iterables in the map function.
  • In case of multiple iterables, the iterator stops after exhausting the shortest iterable.

See the following examples for learning more about map Python function.

An example of converting Fahrenheit to Celsius by using map

In this example, a list of Fahrenheit temperatures is created. A function calCelsius is also created that takes the Fahrenheit value as an argument and converts this to Celsius.

The map function is used where the Fahrenheit list is sent along with providing the calCelsius function. Finally, the returned list is displayed which is the converted temperature from Fahrenheit to Celsius:

See online demo and code

 

Python map F-to-C

You can see, the print function displays the converted temperatures.

Note: You may also use the list comprehension for this.

An example of using multiple iterators in map function

As mentioned earlier, you may use multiple iterators in the map function. In that case, the iterator will stop for the shortest iterator. To demonstrate both these things, I have created two lists in the following example.

The first list contains five items while the second list contains four items. A function is created to multiply the list items with each other.

The map function is used where that function is specified along with passing two lists. See the code and output:

See online demo and code

 

Python map multiple lists

You can see, the resultant list contains four items. As the lowest list is exhausted with four items, the execution stopped.

Using lambda function with Python map example

You may also use the lambda function as working with the map function. The following example shows how to use the lambda function. Again, two lists are created and items of both are added. The sum of items is displayed by using the map function with lambda:

See online demo and code

 

Python map lambda

FYI, the lambda functions are anonymous functions that have no names in Python.