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>Function <code>input</code> behaves quite differently in Python 2 and in Python 3.</p>

<p>This seems to be Python 2. In Python 2, <code>input</code> evaluates entered data as Python code. If only digits are entered, <code>input</code> will return one integer. Converting that to a list is not possible, hence the error.</p>

<p><code>input</code> is unsafe and causes many problems, so it is best to avoid it. Use <code>raw_input</code> instead.</p>

<pre><code>user_input = raw_input("Please enter an 8 digit number: ")
</code></pre>

<p>This will return a string, e.g. <code>'12345678'</code>.</p>

<p>This can be converted to a list. The list will iterate through the string character by character.</p>

<pre><code>digits = list(user_input)   # e.g. ['1', '2', '3', '4', '5', '6', '7', '8']
</code></pre>

<p>But that is not even needed, you can directly do as you did:</p>

<pre><code>numbers = [int(i) for i in user_input]   # e.g. [1, 2, 3, 4, 5, 6, 7, 8]
</code></pre>

<hr>

<p>BTW, the Python 3 version of <code>input</code> is the same as Python 2 <code>raw_input</code>.</p>

python