It takes following arguments
|function||Function over which items in iterable will be applied.|
|iterable||Iterable whose items will be applied to the function, like a list.|
We can also pass multiple iterables as input to map().
Operation and Returns
- It applies the given function to each item in the iterable and returns the response a result.
- Value returned by map() function if of type map object, which can be converted to an iterable, like list.
Lets take a string as input which contains a few numbers and we would like to convert it into a list of integers.
>>> some_list = input().split() 1 2 3 4 5 >>> some_list ['1', '2', '3', '4', '5'] >>>
List items are still string. So, we can use map() to convert them to integers.
>>> map_response = map(int, some_list) >>> map_response <map object at 0x108242310> >>>
Here, response we get from map() is a map object. We can convert it to list.
>>> map_response_list = list(map_response) >>> map_response_list [1, 2, 3, 4, 5] >>>
In short, it can be written as
>>> my_input_list = list( ... map( ... int, ... input().split() ... ) ... ) 1 2 3 4 5 >>> my_input_list [1, 2, 3, 4, 5] >>>