89DEVs

Python map() function

overview of map()

The map() function is used to apply a specified function to all elements of an iterable.

use of map()

The map() function applies the function specified in the first argument to each element of the iterable specified in the second argument. # use of map() function numbers = [-1, 2, 4, -5, -2] numbers = map(abs, numbers) numbers = list(numbers) print(numbers) The map() function is used to apply the abs() function to each element of the numbers list. Then the numbers are converted back to a list using the list() function. The result is a list of absolute numbers without any negative number. [1, 2, 4, 5, 2]

syntax of map()

The syntax of the map() function is: map(function, iterable[,...])

arguments of map()

The map() function accepts two arguments: The first argument is the function to be applied to each element of the iterable and the second argument is the iterable like a set or list. It is possible to pass more than one iterable to the map() function.

return value of map()

The map() function returns an object of type map. It can be passed to other functions like list() or set() to convert it back to an iterable.

                
        

Summary


Click to jump to section