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, sets, etc.).
- The map function returns iterable e.g. a list after applying the given function – yielding the result.
To make it clearer, consider this example.
Suppose, we have a list of mixed items i.e. strings, integers, and float.
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.
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:
The complete code and output will be:
lst_mixed = ['The','map','function',9.9, 50] str_join_map = " ".join(map(str, lst_mixed)) print ("The output:",str_join_map)
How to use the map function?
The general syntax of using the Python map function is:
A few points about the map function
- The map function takes two parameters: a function and an 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 the case of multiple iterables, the iterator stops after exhausting the shortest iterable.
See the following examples to learn more about the Python map() 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:
#An example of map method for Fahrenheit to Celsius def calCelsius(F): C = ((5.0/9.0)*(F - 32)) return C fah_temp = [98.5, 101,102,203,104] conv_temp = (map(calCelsius, fah_temp)) #Convert the returned map object to list Cel_lst = list(conv_temp) print(Cel_lst)
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:
#An example of map method for multiple lists def calctwolsts(num1,num2): return num1*num2 L1 = [2, 4, 6, 8, 10] L2 = [5, 10, 15, 20] L3 = map(calctwolsts, L1, L2) print(list(L3))
You can see that 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 while 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:
#An example of map with lambda function Lst1 = [10, 20, 30, 40] Lst2 = [50, 60, 70, 80] sum_Lsts = map(lambda num1,num2: num1+num2, Lst1,Lst2) print("The sum of two lists", list(sum_Lsts))