title: Simple idiom to break an n-long list into k-long chunks, when n % k > 0?
body: <p>In Python, it is easy to break an <em>n</em>-long list into <em>k</em>-size chunks if <em>n</em> is a multiple of <em>k</em> (IOW, <code>n % k == 0</code>).  Here's my favorite approach (straight from the <a href="http://docs.python.org/library/functions.html#zip" rel="nofollow noreferrer"><strong>docs</strong></a>):</p>
  
  <pre><code>&gt;&gt;&gt; k = 3
  &gt;&gt;&gt; n = 5 * k
  &gt;&gt;&gt; x = range(k * 5)
  &gt;&gt;&gt; zip(*[iter(x)] * k)
  [(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11), (12, 13, 14)]
  </code></pre>
  
  <p>(The trick is that <code>[iter(x)] * k</code> produces a list of <em>k</em> references to <em>the same iterator</em>, as returned by <code>iter(x)</code>.  Then <code>zip</code> generates each chunk by calling each of the <em>k</em> copies of the iterator exactly once.  The <code>*</code> before <code>[iter(x)] * k</code> is necessary because <code>zip</code> expects to receive its arguments as "separate" iterators, rather than a list of them.)</p>
  
  <p>The main shortcoming I see with this idiom is that, when <em>n</em> is not a multiple of <em>k</em> (IOW, <code>n % k &gt; 0</code>), the left over entries are just left out; e.g.:</p>
  
  <pre><code>&gt;&gt;&gt; zip(*[iter(x)] * (k + 1))
  [(0, 1, 2, 3), (4, 5, 6, 7), (8, 9, 10, 11)]
  </code></pre>
  
  <p>There's an alternative idiom that is slightly longer to type, produces the same result as the one above when <code>n % k == 0</code>, and has a more acceptable behavior when <code>n % k &gt; 0</code>:</p>
  
  <pre><code>&gt;&gt;&gt; map(None, *[iter(x)] * k)
  [(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11), (12, 13, 14)]
  &gt;&gt;&gt; map(None, *[iter(x)] * (k + 1))
  [(0, 1, 2, 3), (4, 5, 6, 7), (8, 9, 10, 11), (12, 13, 14, None)]
  </code></pre>
  
  <p>At least, here the left over entries are retained, but the last chunk gets padded with <code>None</code>.  If one just wants a different value for the padding, then <a href="http://docs.python.org/library/itertools.html#itertools.izip_longest" rel="nofollow noreferrer"><code>itertools.izip_longest</code></a> solves the problem.</p>
  
  <p>But suppose the desired solution is one in which the last chunk is left unpadded, i.e.</p>
  
  <pre><code>[(0, 1, 2, 3), (4, 5, 6, 7), (8, 9, 10, 11), (12, 13, 14)]
  </code></pre>
  
  <p>Is there a <em>simple</em> way to modify the <code>map(None, *[iter(x)]*k)</code> idiom to produce this result?</p>
  
  <p>(Granted, it is not difficult to solve this problem by writing a function (see, for example, the many fine replies to <a href="https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python">How do you split a list into evenly sized chunks?</a> or <a href="https://stackoverflow.com/questions/434287/what-is-the-most-pythonic-way-to-iterate-over-a-list-in-chunks">What is the most &quot;pythonic&quot; way to iterate over a list in chunks?</a>).  Therefore, a more accurate title for this question would be "How to salvage the <code>map(None, *[iter(x)]*k)</code> idiom?", but I think it would baffle a lot of readers.)</p>
  
  <p>I was struck by how easy it is to break a list into even-sized chunks, and how difficult (<em>in comparison!</em>) it is to get rid of the unwanted padding, even though the two problems seem of comparable complexity.</p>