def rle_subseg(rle, start, end):
    """Return a sub segment of an rle list."""
    l = []
    x = 0
    for a, run in rle:
        if start:
            if start >= run:
                start -= run
                x += run
                continue
            x += start
            run -= start
            start = 0
        if x >= end:
            break
        if x + run > end:
            run = end - x
        x += run
        l.append((a, run))
    return l
