itertools for analysts
itertools has small tools that replace whole loops:
accumulate(xs): running totals, [1, 2, 3] gives 1, 3, 6.
groupby(rows, key=...): groups of consecutive items with the same key. Sort by the same key first, or one region can turn up as two groups.
combinations(people, 2): every pair, without repeats or the same person twice.
product(sizes, colors): every combination of one item from each list.
chain(a, b, c): one sequence after another, and chain.from_iterable(lists) flattens a list of lists.
islice(it, 5): the first five items of any iterator.
zip_longest(a, b, fillvalue="-"): zip that keeps going to the longer list.
They return lazy iterators: wrap them in list(...) to see or store the values, and remember that an iterator is used up once you have looped over it.
from itertools import accumulate, combinations
list(accumulate([120, 80, 45])), list(combinations(["Ana", "Ben", "Chi"], 2))
Out: ([120, 200, 245], [('Ana', 'Ben'), ('Ana', 'Chi'), ('Ben', 'Chi')]).