itertools
The itertools
library contains iterable functions inspired by those available in functional languages. All of these are iterable and have been constructed in such a way that only a minimal amount of memory is required to process even the largest of datasets. While you can easily write most of these functions yourself, I would still recommend using the ones available in the itertools
library. These are all fast, memory efficient, and—perhaps more importantly—tested. We’re going to explore a few now: accumulate
, chain
, compress
, dropwhile/takewhile
, count
, and groupby
.
accumulate – reduce with intermediate results
The accumulate
function is very similar to the reduce
function, which is why some languages actually have accumulate
instead of reduce
as the folding operator.
The major difference between the two is that the accumulate
function returns the immediate results. This can be useful when summing the results of a company’s...