import itertools


def encode(l):
    """
    Run length encoding of a time series.

    Turns [0, 0, 1, 1, 1, 0, 0, 0, 0] into: [(2, 0), (3, 1), (4, 0)]
    """
    return [[len(list(group)), name] for name, group in itertools.groupby(l)]