Functional Python Programming

(Wang) #1

The Itertools Module


Functional programming emphasizes stateless programming. In Python this leads
us to work with generator expressions, generator functions, and iterables. In this
chapter, we'll look at the itertools library with numerous functions to help us work
with iterable collections.


We introduced iterator functions in Chapter 3, Functions, Iterators, and Generators.
In this chapter, we'll expand on that superficial introduction. We used some related
functions in Chapter 5, Higher-order Functions.


Some of the functions merely behave like they are proper, lazy
Python iterables. It's important to look at the implementation details
for each of these functions. Some of them create intermediate objects,
leading to the potential of consuming a large amount of memory.
Since implementations might change with Python releases, we can't
provide function-by-function advice here. If you have performance or
memory issues, ensure that you check the implementation.

There are a large number of iterator functions in this module. We'll examine some of
the functions in the next chapter. In this chapter, we'll look at three broad groupings
of iterator functions. These are as follows:



  • Functions that work with infinite iterators. These can be applied to any iterable
    or an iterator over any collection; they will consume the entire source.

  • Functions that work with finite iterators. These can either accumulate a
    source multiple times, or they produce a reduction of the source.

  • The tee iterator function which clones an iterator into several copies that can
    each be used independently. This provides a way to overcome the primary
    limitation of Python iterators: they can be used once only.

Free download pdf