software-mariodiana
4/14/2015 - 2:06 PM

Iterate through a list or tuple, allowing each element to be a subset of the sequence.

Iterate through a list or tuple, allowing each element to be a subset of the sequence.

def slice_sequence(sequence, limit):
    """
    Return subsets of sequence, each no larger than limit.
    """
    end = 0
    maxsize = len(sequence)
    while True:
        start = end
        end = limit + end
        end = end if end < maxsize else maxsize
        yield sequence[start: end]
        if end == maxsize:
            break