Suppose I have an input integer 12345
. How can I split it into a list like [1, 2, 3, 4, 5]
?

- 62,466
- 11
- 102
- 153

- 35,524
- 46
- 151
- 243
10 Answers
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:
>>> [int(i) for i in str(12345)]
[1, 2, 3, 4, 5]
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]

- 120,166
- 34
- 186
- 219
-
31In Python3, that would be `list(map(int,str(12345)))` – Serge Stroobandt Aug 26 '17 at 20:51
-
1Or could also be `[*map(int,str(12345))]` – teepee Nov 26 '20 at 18:51
I'd rather not turn an integer into a string, so here's the function I use for this:
def digitize(n, base=10):
if n == 0:
yield 0
while n:
n, d = divmod(n, base)
yield d
Examples:
tuple(digitize(123456789)) == (9, 8, 7, 6, 5, 4, 3, 2, 1)
tuple(digitize(0b1101110, 2)) == (0, 1, 1, 1, 0, 1, 1)
tuple(digitize(0x123456789ABCDEF, 16)) == (15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
As you can see, this will yield digits from right to left. If you'd like the digits from left to right, you'll need to create a sequence out of it, then reverse it:
reversed(tuple(digitize(x)))
You can also use this function for base conversion as you split the integer. The following example splits a hexadecimal number into binary nibbles as tuples:
import itertools as it
tuple(it.zip_longest(*[digitize(0x123456789ABCDEF, 2)]*4, fillvalue=0)) == ((1, 1, 1, 1), (0, 1, 1, 1), (1, 0, 1, 1), (0, 0, 1, 1), (1, 1, 0, 1), (0, 1, 0, 1), (1, 0, 0, 1), (0, 0, 0, 1), (1, 1, 1, 0), (0, 1, 1, 0), (1, 0, 1, 0), (0, 0, 1, 0), (1, 1, 0, 0), (0, 1, 0, 0), (1, 0, 0, 0))
Note that this method doesn't handle decimals, but could be adapted to.

- 6,386
- 2
- 27
- 52
[int(i) for i in str(number)]
or, if do not want to use a list comprehension or you want to use a base different from 10
from __future__ import division # for compatibility of // between Python 2 and 3
def digits(number, base=10):
assert number >= 0
if number == 0:
return [0]
l = []
while number > 0:
l.append(number % base)
number = number // base
return l

- 8,699
- 2
- 32
- 42
-
-
@nd you can put the base of the number inside int like int(i,2) for binary see my post – fabrizioM Dec 15 '09 at 11:29
-
While list(map(int, str(x)))
is the Pythonic approach, you can formulate logic to derive digits without any type conversion:
from math import log10
def digitize(x):
n = int(log10(x))
for i in range(n, -1, -1):
factor = 10**i
k = x // factor
yield k
x -= k * factor
res = list(digitize(5243))
[5, 2, 4, 3]
One benefit of a generator is you can feed seamlessly to set
, tuple
, next
, etc, without any additional logic.

- 159,742
- 34
- 281
- 339
like @nd says but using the built-in function of int to convert to a different base
>>> [ int(i,16) for i in '0123456789ABCDEF' ]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
>>> [int(i,2) for i in "100 010 110 111".split()]
[4, 2, 6, 7]

- 62,466
- 11
- 102
- 153

- 46,639
- 15
- 102
- 119
Using join
and split
methods of strings:
>>> a=12345
>>> list(map(int,' '.join(str(a)).split()))
[1, 2, 3, 4, 5]
>>> [int(i) for i in ' '.join(str(a)).split()]
[1, 2, 3, 4, 5]
>>>
Here we also use map
or a list comprehension to get a list.

- 62,466
- 11
- 102
- 153

- 69,221
- 14
- 89
- 114
-
1This is pointless. Joining a string with `' '` just adds spaces in between for the `.split`, so this is just a convoluted way to make a list of the characters (`list(str(a))` is much simpler) and more direct) - but there is also no reason to create that intermediate list anyway, since the string is already iterable. – Karl Knechtel Oct 14 '22 at 03:03
Another solution that does not involve converting to/from strings:
from math import log10
def decompose(n):
if n == 0:
return [0]
b = int(log10(n)) + 1
return [(n // (10 ** i)) % 10 for i in reversed(range(b))]

- 416
- 5
- 9
Strings are just as iterable as arrays, so just convert it to string:
str(12345)

- 391,730
- 64
- 469
- 606
Simply turn it into a string, split, and turn it back into an array integer:
nums = []
c = 12345
for i in str(c):
l = i.split()[0]
nums.append(l)
np.array(nums)

- 18,379
- 16
- 47
- 61

- 123
- 1
- 4
-
`i.split()[0]` is just `i` as `i` is a character from a string... And then your answer comes down to be the same as the ones posted 13 (!!!) years ago... And you still give a list of strings while the expected is a list of ints – Tomerikoo Jan 16 '23 at 11:53