the itertools module shows the pairwise() method in its recipes. adapting from this recipe, you can get your generator:
from itertools import * def n_apart(iterable, n): a,b = tee(iterable) for count in range(n): next(b) return zip(a,b) def all_but_n_last(iterable, n): return (value for value,dummy in n_apart(iterable, n))
n_apart() returns return pairs of values ββthat are n elements separately in the input iterable, ignoring all pairs. all_but_b_last() returns the first value of all pairs, which accidentally ignores the n last list items.
>>> data = range(10) >>> list(data) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> list(n_apart(data,3)) [(0, 3), (1, 4), (2, 5), (3, 6), (4, 7), (5, 8), (6, 9)] >>> list(all_but_n_last(data,3)) [0, 1, 2, 3, 4, 5, 6] >>> >>> list(all_but_n_last(data,1)) [0, 1, 2, 3, 4, 5, 6, 7, 8]