Splitting integer in Python?


Question

My integer input is suppose 12345, I want to split and put it into an array as 1, 2, 3, 4, 5. How will I be able to do it?

1
57
11/17/2016 4:38:57 PM

Accepted Answer

>>> [int(i) for i in str(12345)]

[1, 2, 3, 4, 5]
98
12/15/2009 11:12:22 AM

return array as string

>>> list(str(12345))
['1', '2', '3', '4', '5']

return array as integer

>>> map(int,str(12345))
[1, 2, 3, 4, 5]

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon