-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
Secret Recipes of the Python Ninja
Beyond just the standard iteration protocol, Python also provides the itertools module. This module provides a number of iterator building blocks that, used singly or in combination, can create specialized iteration tools for efficient looping.
There are three main categories of itertools: infinite iterators, combinatoric iterators, and iterators that terminate on the shortest input sequence.
Infinite iterators return values repeatedly until a terminating condition is reached:
The count(start=0, step=1) function returns evenly spaced values that start at the start argument provided. Stepping is provided to allow skipping values. This function is frequently used with map() to generate consecutive data points. When used with zip(), it can be used to add sequence numbers:

count() function from the itertools module in line 54.5 and a stepping...