How to convert a list by mapping an element into multiple elements in python?
<p>For example,
how to convert [1, 5, 7] into [1,2,5,6,7,8] into python?
[x, x+1 for x in [1,5,7]] can't work for sure...</p>

<p>And you can always overcomplicate the problem with <a href="https://docs.python.org/2/library/operator.html" rel="nofollow"><code>operator</code></a>, <a href="https://docs.python.org/2/library/itertools.html#itertools.imap" rel="nofollow"><code>imap()</code></a>, <a href="https://docs.python.org/2/library/functools.html#functools.partial" rel="nofollow"><code>partial()</code></a>, <a href="https://docs.python.org/2/library/itertools.html#itertools.izip" rel="nofollow"><code>izip()</code></a> and <a href="https://docs.python.org/2/library/itertools.html#itertools.chain" rel="nofollow"><code>chain()</code></a>:</p>

<pre><code>&gt;&gt;&gt; from itertools import chain, izip, imap
&gt;&gt;&gt; from operator import add
&gt;&gt;&gt; from functools import partial
&gt;&gt;&gt; 
&gt;&gt;&gt; l = [1, 5, 7]
&gt;&gt;&gt;
&gt;&gt;&gt; list(chain(*izip(l, imap(partial(add, 1), l))))
[1, 2, 5, 6, 7, 8]
</code></pre>

<p>What happens here:</p>

<ul>
<li>we make an iterator over <code>l</code> that applies an <a href="https://docs.python.org/2/library/operator.html#operator.add" rel="nofollow"><code>add</code> function</a> with <code>1</code> as an argument</li>
<li>we zip the initial list with the iterator returned by <code>imap()</code> to produce pairs of x, x+1 values</li>
<li>we <a href="http://stackoverflow.com/a/953097/771848">flatten the list with <code>chain()</code></a> and convert it to the list to see the result</li>
</ul>

python