import itertools


def flatten_seq(seq, levels=1):
    """
    Flatten a nested sequence, up to ``levels`` levels.
    """
    if levels == 0:
        return seq
    else:
        seq = list(itertools.chain.from_iterable(seq))
        return flatten_seq(seq, levels=levels - 1)