4.2 Working with iterables
As noted in the previous chapters, Python’s for
statement works with iterables, including Python’s rich variety of collections. When working with materialized collections such as tuples, lists, maps, and sets, the for
statement involves the explicit management of state.
While this strays from purely functional programming, it reflects a necessary optimization for Python. The state management is localized to an iterator object that’s created as a part of the for
statement evaluation; we can leverage this feature without straying too far from pure, functional programming. If, for example, we use the for
statement’s variable outside the indented body of the statement, we’ve strayed from purely functional programming by leveraging this state control variable.
We’ll return to this in Chapter 6, Recursions and Reductions. It’s an important topic, and we’ll just scratch the surface in this section with a...