Suppose I have an input integer 12345
. How can I split it into a list like [1, 2, 3, 4, 5]
?
Advertisement
Answer
Convert the number to a string so you can iterate over it, then convert each digit (character) back to an int inside a list-comprehension:
JavaScript
x
4
1
>>> [int(i) for i in str(12345)]
2
3
[1, 2, 3, 4, 5]
4