Converting input string of integers to list, then to ints in python?
<p>I want the user to input an integer (6 digits long, so 123456 rather than just 1), and then convert that input into a list, <code>[1,2,3,4,5,6]</code>.</p>

<p>I tried this:</p>

<pre><code>user_input = list(input("Please enter an 8 digit number")
numbers = [int(i) for i in user_input]
</code></pre>

<p>I want to be able to perform mathematical stuff with the numbers list, but I keep getting the error "int is not iterable". To be frank, I'm not entirely sure what I'm doing, or sure that the "numbers = [...] " is even necessary, or if it should just be <code>numbers = user_input</code>. Trying <code>numbers = [i for i in user_input]</code> gets the same error.</p>

<p>Also, I realise I could either run a loop to get each number from the user, or ask them to use commas in between each in order to use the <code>.split(",")</code>, but I'd rather not as it seems messy to the user.</p>

<p>Edit: I've been switching things between versions, so sorry for any confusion. This was written in 2.7, though I intend to use Python 3.</p>

<p>In Python 2.7, <code>input()</code> returns an integer. To read input as a string use the function <code>raw_input()</code> instead. Alternatively, you can switch to Python 3 and where <code>input()</code> always returns a string.</p>

<p>Also your solution isn't very neat in case the user is providing numbers with more than 1 digits. For example the string "123" can be interpreted as [1, 2, 3], [12, 3] and so on. </p>

<p>A neat solution is to ask the user to provide the input separated by spaces as follows x_1, x_2, ... x_n</p>

<p>Then your code in Python 3.0 will look like</p>

<pre><code>lst = [int(x) for x in input().split()]
</code></pre>

<p>And for Python 2.7</p>

<pre><code>lst = [int(x) for x in raw_input().split()]
</code></pre>

python