This is precisely what the pairwise
itertools recipe is for, for n=2
that is.
from itertools import tee
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
return zip(a, b)
Demo:
>>> b = [1.0,1.5,2.0,2.5,3.0]
>>> list(pairwise(b))
[(1.0, 1.5), (1.5, 2.0), (2.0, 2.5), (2.5, 3.0)]
If you are looking for variable group sizes, see user2357112's answer (I like the approach), or more generally you can implement a sliding window iterator and take slices of which there are many approaches.
As an aside, a possibly poorly performing but amusing one-line window you could slice (to control the overlap) that isn't on the linked question would be this, using the new yield from
syntax to combine generators.
from itertools import tee, islice
def roll_window(it, sz):
yield from zip(*[islice(it, g, None) for g, it in enumerate(tee(it, sz))])
Demo:
>>> b = [1.0,1.5,2.0,2.5,3.0, 3.5, 4.0, 4.5]
>>> list(islice(window(b, 3), None, None, 2))
[(1.0, 1.5, 2.0), (2.0, 2.5, 3.0), (3.0, 3.5, 4.0)]