next up previous contents
Next: Filter. Up: MapReduce and Previous: MapReduce and

Map.

map(function, sequence) calls function(item) for each of the sequence's items and returns a list of the return values. For example, to compute some cubes:

        >>> map(lambda x: x*x*x, range(1, 11))
        [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
        >>>

More than one sequence may be passed; the function must then have as many arguments as there are sequences and is called with the corresponding item from each sequence (or None if some sequence is shorter than another). If None is passed for the function, a function returning its argument(s) is substituted.

Combining these two special cases, we see that map(None, list1, list2) is a convenient way of turning a pair of lists into a list of pairs. For example:

        >>> seq = range(8)
        >>> map(None, seq, map(lambda x: x*x, seq))
        [(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25), (6, 36), (7, 49)]
        >>>



guido@cwi.nl