ScanSkill

map

This map() function applies the function to every item of an iterable and returns a list of the results.

Syntax

map(function, iterable [, …])

Here,

  • function: Required. The function that is used for creating a list.
  • iterable: Required. One or multiple(comma-separated) iterable object(s).

Note: If additional iterable arguments, the function must take that many arguments and then 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 the function is None, the identity function is assumed.

If there are multiple arguments, map() returns a list consisting of tuples containing the corresponding items from all iterables. The iterable arguments may be a sequence or any iterable object; the result is always a list.

Examples

>>> 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)]