Linux/Unix > Python Essential Reference, Second Edition > 6. Functions and Functional Programming > map() , zip() , reduce() , and filter() | See All Titles

< BACK | Make Note | Bookmark | CONTINUE >

155117184014003188065099048180054212144238241179195140058238110137104017178093174198134036

### `map()`, `zip()`, `reduce()`, and `filter()`

The `t` `= map(``func` `,` `s` `)` function applies the function `func` to each of the elements in `s` and returns a new list `t` . Each element of `t` is `t` `[``i` `] = func(``s` `[``i` `])`. The function given to `map()` should require only one argument. For example:

    a = [1, 2, 3, 4, 5, 6]
    def foo(x):
        return 3*x

    b = map(foo,a)   # b = [3, 6, 9, 12, 15, 18]

Alternatively, this could be calculated using an anonymous function as follows:

    b = map(lambda x: 3*x, a)   # b = [3, 6, 9, 12, 15, 18]

The `map()` function can also be applied to multiple lists such as `t` `= map(``func` `,` `s1` `,` `s2` `, ...,` `sn` `)`. In this case, each element of `t` is `t` `[``i` `] =` `func s1` `[``i` `],` `s2` `[``i` `], ...,` `sn` `[``i` `])`, and the function given to `map()` must accept the same number of arguments as the number of lists given. The result has the same number of elements as the longest list in `s1` `,` `s2` `, ...` `sn` . During the calculation, short lists are extended with values of `None` to match the length of the longest list, if necessary.

If the function is set to `None`, the identity function is assumed. If multiple lists are passed to `map(None,` `s1` `,` `s2` `, ...` `sn` `)`, the function returns a list of tuples in which each tuple contains an element from each list. For example:

    a = [1,2,3,4]
    b = [100,101,102,103]
    c = map(None, a, b)   # c = [(1,100), (2,101), (3,102), (4,103)]

As an alternative to `map()`, a list of tuples can also be created using the `zip(``s1` `,` `s2` `, ...,` `sn` `)` function. `zip()` takes a collection of sequences and returns a new list `t` in which each element of `t` is `t` `[``i` `] = (``s1` `[``i` `],` `s2` `[``i` `], ...,` `sn` `[``i` `])`. Unlike `map()`, `zip()` truncates the length of `t` to the shortest sequence in `s1` `,` `s2` `, ...` `sn` . For example:

    d = [1,2,3,4,5,6,7]
    e = [10,11,12]
    f = zip(d,e)   # f = [(1,10), (2,11), (3,12)]

The `reduce(``func` `,` `s` `)` function collects information from a sequence and returns a single value (for example, a sum, maximum value, and so on). `reduce()` works by applying the function `func` to the first two elements of `s` . This value is then combined with the third element to yield a new value. This result is then combined with the fourth element, and so forth until the end of the sequence. The function `func` must accept two arguments and return a single value. For example:

    def sum(x,y):
        return x+y

    b = reduce(sum, a)   # b = (((1+2)+3)+4) = 10

The `filter(``func` `,``s` `)` function filters the elements of `s` using a filter function `func` `()` that returns true or false.A new sequence is returned consisting of all elements `x` of `s` for which `func` `(``x` `)` is true. For example:

    c = filter(lambda x: x < 4, a)   # c = [1, 2, 3]

If `func` is set to `None`, the identity function is assumed and `filter()` returns all elements of `s` that evaluate to true.

| Last updated on 3/28/2002 Python Essential Reference, Second Edition, © 2002 New Riders Publishing

< BACK | Make Note | Bookmark | CONTINUE >

© 2002, O'Reilly & Associates, Inc.