title: split a generator/iterable every n items in python (splitEvery)
body: <p>I'm trying to write the Haskell function 'splitEvery' in Python. Here is it's definition:</p>
  <pre><code>splitEvery :: Int -&gt; [e] -&gt; [[e]]
      @'splitEvery' n@ splits a list into length-n pieces.  The last
      piece will be shorter if @n@ does not evenly divide the length of
      the list.
  </code></pre>
  <p>The basic version of this works fine, but I want a version that works with generator expressions, lists, and iterators. <em>And</em>, if there is a generator as an input it should return a generator as an output!</p>
  <h2>Tests</h2>
  <pre><code># should not enter infinite loop with generators or lists
  splitEvery(itertools.count(), 10)
  splitEvery(range(1000), 10)
  
  # last piece must be shorter if n does not evenly divide
  assert splitEvery(5, range(9)) == [[0, 1, 2, 3, 4], [5, 6, 7, 8]]
  
  # should give same correct results with generators
  tmp = itertools.islice(itertools.count(), 10)
  assert list(splitEvery(5, tmp)) == [[0, 1, 2, 3, 4], [5, 6, 7, 8]]
  </code></pre>
  <h2>Current Implementation</h2>
  <p>Here is the code I currently have but it doesn't work with a simple list.</p>
  <pre><code>def splitEvery_1(n, iterable):
      res = list(itertools.islice(iterable, n))
      while len(res) != 0:
          yield res
          res = list(itertools.islice(iterable, n))
  </code></pre>
  <p>This one doesn't work with a generator expression (thanks to jellybean for fixing it):</p>
  <pre><code>def splitEvery_2(n, iterable): 
      return [iterable[i:i+n] for i in range(0, len(iterable), n)]
  </code></pre>
  <p>There has to be a simple piece of code that does the splitting. I know I could just have different functions but it seems like it should be and easy thing to do. I'm probably getting stuck on an unimportant problem but it's really bugging me.</p>
  <hr />
  <p>It is similar to grouper from <a href="http://docs.python.org/library/itertools.html#itertools.groupby" rel="noreferrer">http://docs.python.org/library/itertools.html#itertools.groupby</a> but I don't want it to fill extra values.</p>
  <pre><code>def grouper(n, iterable, fillvalue=None):
      &quot;grouper(3, 'ABCDEFG', 'x') --&gt; ABC DEF Gxx&quot;
      args = [iter(iterable)] * n
      return izip_longest(fillvalue=fillvalue, *args)
  </code></pre>
  <p>It does mention a method that truncates the last value. This isn't what I want either.</p>
  <blockquote>
  <p>The left-to-right evaluation order of the iterables is guaranteed. This makes possible an idiom for clustering a data series into n-length groups using izip(*[iter(s)]*n).</p>
  </blockquote>
  <pre><code>list(izip(*[iter(range(9))]*5)) == [[0, 1, 2, 3, 4]]
  # should be [[0, 1, 2, 3, 4], [5, 6, 7, 8]]
  </code></pre>