Function map()
- VC Healy
- May 23, 2020
- 1 min read
Updated: May 24, 2020
Applies function to every item of an iterable and returns a list of the results.
map(function, iterable [, …])
function
Required. A function that is used to create a new list.
iterable
Required. An iterable object or multiple comma-separated iterable objects.
If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel.
If one iterable is shorter than another it is assumed to be extended with None items.
If function is None, the identity function is assumed
With multiple iterables, the iterator stops when the shortest iterable is exhausted.
For cases where the function inputs are already arranged into argument tuples, see itertools.starmap().
>>> map(lambda x: x+x, (1, 2, 3))
[2, 4, 6]
>>> map(lambda x, y: x/y, (1, 4, 9), (1, 2, 3))
[1, 2, 3]
>>> map(lambda x, y, z: x+y+z, (1, 2, 3), (1, 4, 9), (1, 16, 27))
[3, 22, 39]
>>> map(None, [True, False])
[True, False]
>>> map(None, ['a', 'b'], [1, 2])
[('a', 1), ('b', 2)]
>>> map(None, ['a', 'b'], [1, 2, 3])
[('a', 1), ('b', 2), (None, 3)]
Comentários