I'm having trouble trying to read the numbers (0 - 10) into a list in Python 3 using CSV file and in a column format.
<p>I first wrote the numbers in a column into a CSV file but having trouble with the reading part. I want to make into one list and make sure that the numbers are converted into ints</p>

<pre><code>f = open('numbers.csv', 'r')
with f:
    reader = csv.reader(f)
    for column in reader:
        print(column)
</code></pre>

<p>This is what I wrote for my code and here is my output but how do I make it into a list and convert the numbers into integers?</p>

<p>Column:</p>

<pre><code>['0']
['1']
['2']
['3']
['4']
['5']
['6']
['7']
['8']
['9']
['10']
</code></pre>

<p>You just need to iterate over each <code>row</code> in your <code>file</code>, cast the first <code>column</code> to <code>int</code> using <code>int()</code> and then add to a <code>list</code>. Here is compact version:</p>

<pre><code>with open('numbers.csv', 'r') as numbers_file:
    reader = csv.reader(numbers_file)
    int_list = [int(row[0]) for row in reader]

print int_list
</code></pre>

<p><strong>EDIT</strong>:</p>

<p>If you want a <code>column</code> list or <code>nested</code> list, just loop over both <code>columns</code> and <code>rows</code> for the <em>general</em> case:</p>

<pre><code>int_list = [[int(col) for col in row] for row in reader]
</code></pre>

<p>or in your <em>particular</em> case where you just want the <code>first/only</code> element of each row, just cast the <code>element</code> to a <code>list</code>:</p>

<pre><code>int_list = [[int(row[0])] for row in reader]
</code></pre>

python numbers