Chapter 8
Function Name Arguments Results
dotproduct (vec1, vec2) This is the essential definition of a dot
product. Multiply two vectors and find the
sum of the result.
flatten (listOfLists) This flattens one level of nesting. This chains
the various lists together into a single list.
repeatfunc (func,
times=None,
*args)
This calls to func repeatedly with specified
arguments.
pairwise (iterable): s -> (s0,s1), (s1,s2), (s2, s3).
grouper (iterable, n,
fillvalue=None)
Collect data into fixed length chunks or
blocks.
roundrobin (*iterables) roundrobin('ABC', 'D', 'EF') -->
A D
E B F C
partition (pred,
iterable)
This uses a predicate to partition entries into
False entries and True entries.
unique_
everseen
(iterable,
key=None)
This lists unique elements, preserving order.
Remembers all elements ever seen. unique_
everseen('AAAABBBCCDAABBB') -
-> A B C D.
unique_
justseen
(iterable,
key=None)
This lists unique elements, preserving order.
Remembers only the element just seen.
unique_justseen('AAAABBBCCDAABBB')
- -> A B C D A B.
iter_except (func,
exception,
first=None)
Call a function repeatedly until an exception
is raised. This can be used to iterate until
KeyError or IndexError.
Summary
In this chapter, we've looked at a number of functions in the itertools module.
This library module provides a number of functions that help us to work with
iterators in sophisticated ways.
We've looked at the infinite iterators; these repeat without terminating. These include
the count(), cycle(), and repeat() functions. Since they don't terminate, the
consuming function must determine when to stop accepting values.