Adding data to a Python list
<p>I've just started playing around with python lists, I've written the simple code below expecting the printed file to display the numbers [12,14,16,18,20,22] but only 22 is displayed. Any help would be great.</p>

<pre><code>a=10
b=14
while a &lt;= 20:
    a=a+2
    b=b-1
    datapoints=[]
    datapoints.insert(0,a)
print datapoints
</code></pre>

<p>you can achieve the expected list as output by using the <a href="https://docs.python.org/2/library/functions.html#range" rel="nofollow">range()</a> method. It takes three parameters, start, stop and step. </p>

<pre><code>data_points = range(12, 23, 2)  # range returns list in python 2
print data_points
</code></pre>

<p>Note that, in <code>python 3</code> the <a href="https://docs.python.org/3/library/functions.html#func-range" rel="nofollow">range()</a> is a <a href="https://docs.python.org/3/library/stdtypes.html#range" rel="nofollow">sequence-type</a>. So, you will have to cast it to <code>list</code> in python 3</p>

<pre><code>data_points = list(range(12, 23, 2))  # python 3
print(data_points) 
</code></pre>

python python-2.7