alternately appending elements from two lists
<p>I have three lists with elements :</p>

<pre><code>a = [[0,1],[2,3],...]
b = [[5,6],[7,8],...]

c = []
</code></pre>

<p>I want to append elements from <code>a</code> and <code>b</code> into <code>c</code> to get:</p>

<pre><code>c = [ [0,1],[5,6],[2,3],[7,8],.... ]
</code></pre>

<p>Assuming the two lists are the same length, the most compact way to do this uses <code>itertools.chain</code> and <code>zip</code>.</p>

<pre><code>from itertools import chain

a = [[0,1],[2,3],[10,11],[12,13]]
b = [[5,6],[7,8],[15,16],[17,18]]

c = [*chain(*zip(a, b))]
print(c)
</code></pre>

<p><strong>output</strong></p>

<pre><code>[[0, 1], [5, 6], [2, 3], [7, 8], [10, 11], [15, 16], [12, 13], [17, 18]]
</code></pre>

<hr>

<p>As juanpa.arrivillaga mentions in the comments, that syntax will not work on older versions of Python. Instead, you can do</p>

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

<p>Here's another option, which doesn't use imports or the <code>*</code> splat operator:</p>

<pre><code>c = [u for t in zip(a, b) for u in t]
</code></pre>

<hr>

<p>If you need to handle input lists of unequal length, take a look at the <code>roundrobin</code> function in <a href="https://docs.python.org/3/library/itertools.html#itertools-recipes" rel="nofollow">Itertools Recipes</a>. Eg,</p>

<pre><code>c = list(roundrobin(a, b))
</code></pre>

python list append