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)
is a
function that takes one argument and returns a value.
f
is any iterable.
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)]