117

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

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Hick
  • 35,524
  • 46
  • 151
  • 243

10 Answers10

186

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]
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
luc
  • 41,928
  • 25
  • 127
  • 172
113

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]
YOU
  • 120,166
  • 34
  • 186
  • 219
13

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.

Will Da Silva
  • 6,386
  • 2
  • 27
  • 52
9
[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
nd.
  • 8,699
  • 2
  • 32
  • 42
4

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.

jpp
  • 159,742
  • 34
  • 281
  • 339
2

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]
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
fabrizioM
  • 46,639
  • 15
  • 102
  • 119
0

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.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
  • 1
    This 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
0

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))]
Alexandre V.
  • 416
  • 5
  • 9
-1

Strings are just as iterable as arrays, so just convert it to string:

str(12345)
unwind
  • 391,730
  • 64
  • 469
  • 606
-4

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)
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
The_Data_Guy
  • 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