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>You can combine some ideas from alecxe's answer and what you already had:</p>

<pre><code>&gt;&gt;&gt; import itertools
&gt;&gt;&gt; 
&gt;&gt;&gt; a = [1, 5, 7]
&gt;&gt;&gt; b = ((x, x+1) for x in a)
&gt;&gt;&gt; 
&gt;&gt;&gt; list(itertools.chain(*b))
[1, 2, 5, 6, 7, 8]
</code></pre>

<p>What I have done is :</p>

<ol>
<li><p>Define in <code>b</code> a <a href="https://docs.python.org/3/tutorial/classes.html#generator-expressions" rel="nofollow">generator expression</a> which allows us to have (something that looks like) a tuple that would have looked like <code>((1, 2), (5, 6), (7, 8))</code> but without evaluating it right away. It would have also worked with a list comprehension.</p></li>
<li><p><a href="https://docs.python.org/3/tutorial/controlflow.html#unpacking-argument-lists" rel="nofollow">Unpack <code>b</code> in the argument list</a> of itertools.chain(). It would be the equivalent of <code>itertools.chain((1, 2), (5, 6), (7, 8))</code>. That function concatenates its arguments.</p></li>
<li><p>Use list() to create a list from the return value of the <code>itertools.chain()</code> function since it's an iterator.</p></li>
</ol>

<p>This could also have worked without any intermediate step:</p>

<pre><code>&gt;&gt;&gt; import itertools
&gt;&gt;&gt; list(itertools.chain(*((x, x+1) for x in [1, 5, 7])))
[1, 2, 5, 6, 7, 8]
</code></pre>

<p>But "Simple is better than complex"</p>

<p>I hope this helps.</p>

<p>I would have put more links if I had more reputation, sorry.</p>

python