zip unknown number of lists with Python for more than one list
<p>I need to do something very similar to what was asked here <a href="http://stackoverflow.com/questions/5938786/how-would-you-zip-an-unknown-number-of-lists-in-python">How would you zip an unknown number of lists in Python?</a>, but in a more general case.</p>

<p>I have the following set up:</p>

<pre><code>a = [['a', '0', 'b', 'f', '78']]
b = [['3', 'w', 'hh', '12', '8']]
c = [['g', '7', '1', 'a0', '9'], ['45', '4', 'fe', 'h', 'k']]
</code></pre>

<p>I need to zip these lists together to obtain:</p>

<pre><code>abc =  [['a', '3', 'g', '45'], ['0', 'w', '7', '4'], ['b', 'hh', '1', 'fe'], ['f', '12', 'a0', 'h'], ['78', '8', '9', 'k']]
</code></pre>

<p>which I can generate with:</p>

<pre><code>zip(a[0], b[0], c[0], c[1])
</code></pre>

<p>But the lists <code>a,b,c</code> contain a number of sublists that will vary for successive runs, so this "manual" way of expanding them won't work.</p>

<p>The closest I can get is:</p>

<pre><code>zip(a[0], b[0], *c)
</code></pre>

<p>Since unpacking a list with <code>*</code> in any other position than the last is not allowed, the "ideal" expression:</p>

<pre><code>zip(*a, *b, *c)
</code></pre>

<p>does not work.</p>

<p>How could I zip together a number of lists with an unknown number of sublists?</p>

<p><code>itertools.chain</code> to the rescue:</p>

<pre><code>import itertools

z = list(zip(*itertools.chain(a, b, c)))
</code></pre>

python list zip