(continued from previous page) 11 The module’s functions fall into a few broad classes: The$_{itertools}$module contains a number of commonly-used iterators as well as functions for combining several iterators. This section will introduce the module’s contents by showing small examples. You should avoid doing this, though, because an element may be taken from the longer iterators and discarded. This means you can’t go on to use the iterators further because you risk skipping a discarded element. (For a more detailed discussion of sorting, see the sortinghowto.) The any(iter) and all(iter) built-ins look at the truth values of an iterable’s contents. any() returns True if any element in the iterable is a true value, and all() returns True if all of the elements are true values: zip(iterA, iterB, ...) takes one element from each iterable and returns them in a tuple: zip (['a','b','c'], ( 1,2,3))=> ('a',1), ( 'b',2), ( 'c',3) It doesn’t construct an in-memory list and exhaust all the input iterators before returning; instead tuples are constructed and returned only if they’re requested. (The technical term for this behaviour is lazy evaluation.) This iterator is intended to be used with iterables that are all of the same length. If the iterables are of different lengths, the resulting stream will be the same length as the shortest iterable. zip (['a','b'], ( 1,2,3))=> ('a',1), ( 'b',2) 6 The itertools module • Functions that create a new iterator based on an existing iterator. • Functions for treating an iterator’s elements as function arguments. • Functions for selecting portions of an iterator’s output. • A function for grouping an iterator’s output. >>> any([0,1,0]) True >>> any([0,0,0]) False >>> any([1,1,1]) True >>> all([0,1,0]) False >>> all([0,0,0]) False >>> all([1,1,1]) True [769, 2207, 6213, 6431, 7953, 8442, 9828, 9878] >>> sorted(rand_list, reverse = True ) [9878, 9828, 8442, 7953, 6431, 6213, 2207, 769]