title: Iterate over n successive elements of list (with overlapping)
body: <p>The <a href="https://docs.python.org/2.7/library/itertools.html" rel="nofollow">itertools</a> python module implements some basic building blocks for iterators. As they say, "they form an iterator algebra". I was expecting, but I could not find a succinctly way of doing the following iteration using the module. Given a list of ordered real numbers, for example</p>
  
  <pre><code>a = [1.0,1.5,2.0,2.5,3.0]
  </code></pre>
  
  <p>... return a new list (or just iterate) grouping by some <code>n</code> value, say <code>2</code></p>
  
  <pre><code>b = [(1.0,1.5),(1.5,2.0),(2.0,2.5),(2.5,3.0)]
  </code></pre>
  
  <p>The way I found of doing this was as follows. First split the list in two, with evens and odds indexes:</p>
  
  <pre><code>even, odds = a[::2], a[1::2]
  </code></pre>
  
  <p>Then construct the new list:</p>
  
  <pre><code>b = [(even, odd) for even, odd in zip(evens, odds)]
  b = sorted(b + [(odd, even) for even, odd in zip(evens[1:], odds)])
  </code></pre>
  
  <p>In essence, it is similar to a moving mean. </p>
  
  <p><strong>Is there a succinctly way of doing this (with or without itertools)?</strong> </p>
  
  <hr>
  
  <p>PS.:</p>
  
  <p><strong>Application</strong></p>
  
  <p>Imagine the <code>a</code> list as the set of timestamps of some events occurred during an experiment:</p>
  
  <pre><code>timestamp       event
  47.8            1a
  60.5            1b
  67.4            2a
  74.5            2b
  78.5            1a
  82.2            1b
  89.5            2a
  95.3            2b
  101.7           1a
  110.2           1b
  121.9           2a
  127.1           2b
  
  ...
  </code></pre>
  
  <p>This code is being used to segment those events in accord with different temporal windows. Right now I am interested in the data between <code>2</code> successive events; 'n > 2' would be used only for exploratory purposes.</p>