Creating a list of 'N' pairs of a variable
<p>I have a variable <code>x</code>. I need to create a list such as </p>

<pre><code>lst=[0,x,x,2*x,2*x,3*x,3*x,N*x,N*x]
</code></pre>

<p>up to any <code>N</code></p>

<p>Seems like this should be straightforward but I'm kinda stuck. Any help is appreciated.</p>

<p>Respectfully I don't see how this question is a duplicate</p>

<p><strong>update....</strong> </p>

<p>So I did this. </p>

<pre><code>import numpy as np

N=4
lst=[0]
x=1.2

for i in np.arange(1,N+1):
    seed=[1,1]
    for j in seed:
        lst.append(i*x)

 lst= [0, 1.2, 1.2, 2.3999999999999999, 2.3999999999999999, 3.5999999999999996, 3.5999999999999996, 4.7999999999999998, 4.7999999999999998]
</code></pre>

<p>It feels like a terrible hack. </p>

<p>There has to be a more elegant solution.</p>

<p>you can do this like this</p>

<pre><code>&gt;&gt;&gt; n=4
&gt;&gt;&gt; x=1.2
&gt;&gt;&gt; lst=[0,x,x]
&gt;&gt;&gt; lst.extend( i*x for i in range(2,n+1) for _ in range(2) )
&gt;&gt;&gt; lst
[0, 1.2, 1.2, 2.4, 2.4, 3.5999999999999996, 3.5999999999999996, 4.8, 4.8]
&gt;&gt;&gt; 
</code></pre>

<p>EDIT</p>

<p>or with one list comprehension and nothing else</p>

<pre><code>&gt;&gt;&gt; n=4
&gt;&gt;&gt; x=1.2
&gt;&gt;&gt; lst=[ i*x for i in range(n+1) for _ in range(2 if i else 1) ]
&gt;&gt;&gt; lst
[0.0, 1.2, 1.2, 2.4, 2.4, 3.5999999999999996, 3.5999999999999996, 4.8, 4.8]
&gt;&gt;&gt; 
</code></pre>

<p>(note use xrange if you are in python 2)</p>

python