# 5.14 Filter, Map and Reduce¶

- Built-in `filter` and `map` functions also perform filtering and mapping.

### Filtering a Sequence’s Values with the Built-In `filter` Function¶

In [1]:

    numbers = [10, 3, 7, 1, 9, 4, 2, 8, 5, 6]

In [2]:

    def is_odd(x):
        """Returns True only if x is odd."""
        return x % 2 != 0

In [3]:

    list(filter(is_odd, numbers))

Out[3]:

    [3, 7, 1, 9, 5]

- Functions are objects that you can assign to variables, pass to other functions and return from functions.
- Functions that receive other functions as arguments are a functional-style capability called higher-order functions.
- `filter`’s first argument must be a function that receives one argument and returns `True` if the value should be included in the result.
- Higher-order functions may also return a function as a result.
- `filter` returns an iterator, so `filter`’s results are not produced until you iterate through them—lazy evaluation.

In [4]:

    [item for item in numbers if is_odd(item)]

Out[4]:

    [3, 7, 1, 9, 5]

### Using a `lambda` Rather than a Function¶

- For simple functions like `is_odd` that `return` only a single expression’s value, you can use a lambda expression (or simply a lambda) to define the function inline.

In [5]:

    list(filter(lambda x: x % 2 != 0, numbers))

Out[5]:

    [3, 7, 1, 9, 5]

- A lambda expression is an _anonymous function
- Begins with the `lambda` keyword followed by a comma-separated parameter list, a colon (`:`) and an expression.
- A `lambda` implicitly returns its expression’s value.

### Mapping a Sequence’s Values to New Values¶

In [6]:

    numbers

Out[6]:

    [10, 3, 7, 1, 9, 4, 2, 8, 5, 6]

In [7]:

    list(map(lambda x: x ** 2, numbers))

Out[7]:

    [100, 9, 49, 1, 81, 16, 4, 64, 25, 36]

- Function `map`’s first argument is a function that receives one value and returns a new value.
- equivalent list comprehension:

In [8]:

    [item ** 2 for item in numbers]

Out[8]:

    [100, 9, 49, 1, 81, 16, 4, 64, 25, 36]

### Combining `filter` and `map`¶

In [9]:

    list(map(lambda x: x ** 2,
             filter(lambda x: x % 2 != 0, numbers)))

Out[9]:

    [9, 49, 1, 81, 25]

- Equivalent list comprehension:

In [10]:

    [x ** 2 for x in numbers if x % 2 != 0]

Out[10]:

    [9, 49, 1, 81, 25]

### Reduction: Totaling the Elements of a Sequence with `sum`¶

- Reductions process a sequence’s elements into a single value.
  - E.g., `len`, `sum`, `min` and `max`.
- Can create custom reductions using the `functools` module’s `reduce` function.

©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 5 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.

DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.