Next / Previous / Contents / TCC Help System / NM Tech homepage

20.25. map(): Apply a function to each element of an iterable

The purpose of this function is to perform some operation on each element of an iterable. It returns a list containing the result of those operations. Here is the general form:

map(f, S)
>>> def add100(x):
...     return x+100
... 
>>> map(add100, (44,22,66))
[144, 122, 166]

To apply a function with multiple arguments to a set of sequences, just provide multiple iterables as arguments, like this.

>>> def abc(a, b, c):
...     return a*10000 + b*100 + c
... 
>>> map(abc, (1, 2, 3), (4, 5, 6), (7, 8, 9))
[10407, 20508, 30609]

If you pass None as the first argument, Python uses the identity function to build the resulting list. This is useful if you want to build a list of tuples containing items from two or more iterables.

>>> map(None, range(3))
[0, 1, 2]
>>> map(None, range(3), 'abc', [44, 55, 66])
[(0, 'a', 44), (1, 'b', 55), (2, 'c', 66)]