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>Another very simple approach using <em>string slicing</em> (and <strong>most performance efficient</strong>) as:</p>

<pre><code>&gt;&gt;&gt; a = [[0,1],[2,3]]
&gt;&gt;&gt; b = [[5,6],[7,8]]
&gt;&gt;&gt; c = a + b # create a list with size = len(a) + len(b)
&gt;&gt;&gt; c[::2], c[1::2] = a, b  # alternately insert the value
&gt;&gt;&gt; c
[[0, 1], [5, 6], [2, 3], [7, 8]]
</code></pre>

<p>Below is the comparison of results with <code>timeit</code> for the answers mentioned here (Python version: 2.7):</p>

<ol>
<li><p>Using <em>string slicing</em>: 0.586 usec per loop</p>

<pre><code>moin@moin-pc:~$ python -m "timeit" -s "a = [[0,1],[2,3]]; b = [[5,6],[7,8]];" "c = a + b; c[::2], c[1::2] = a, b"
1000000 loops, best of 3: 0.586 usec per loop
</code></pre></li>
<li><p>Using <code>itertools.chain()</code>: 1.89 usec per loop </p>

<pre><code>moin@moin-pc:~$ python -m "timeit" -s "from itertools import chain; a = [[0,1],[2,3]]; b = [[5,6],[7,8]];" "c = list(chain(*zip(a, b)))"
1000000 loops, best of 3: 1.89 usec per loop
</code></pre></li>
<li><p>Using <code>reduce()</code>: 0.829 usec per loop</p>

<pre><code>moin@moin-pc:~$ python -m "timeit" -s "import operator; a = [[0,1],[2,3]]; b = [[5,6],[7,8]];" "c = reduce(operator.concat, zip(a, b))"
1000000 loops, best of 3: 0.829 usec per loop
</code></pre></li>
<li><p>Using <code>list.extend()</code>: 0.824 usec per loop</p>

<pre><code> moin@moin-pc:~$ python -m "timeit" -s "a = [[0,1],[2,3]]; b = [[5,6],[7,8]]; c=[]" "for pair in zip(a,b): c.extend(pair)"
 1000000 loops, best of 3: 0.824 usec per loop
</code></pre></li>
<li><p>Using <code>list.append()</code> twice: 1.04 usec per loop</p>

<pre><code>moin@moin-pc:~$ python -m "timeit" -s "a = [[0,1],[2,3]]; b = [[5,6],[7,8]]; c=[]" "for a_element, b_element in zip(a, b): c.append(a_element); c.append(b_element)"
1000000 loops, best of 3: 1.04 usec per loop
</code></pre></li>
</ol>

python list append