+ 2

Split at integer to a list of its digits

how can you split an integer in python say 15 to separate elements of a list like [1,5]

26th Oct 2018, 5:45 PM
Jacob Mwaniki
Jacob Mwaniki - avatar
5 Réponses
+ 3
[int(i) for i in str(num)] and [i for i in input()] are already enough: str is an iterable so you don't need a list; and input comes automatically as str.
26th Oct 2018, 11:09 PM
HonFu
HonFu - avatar
+ 6
def split_int(n): lst = [] while n: n, m = divmod(n, 10) lst = [m] + lst return lst print(split_int(15)) print(list(map(int, str(15))))
26th Oct 2018, 6:03 PM
Mert Yazıcı
Mert Yazıcı - avatar
+ 3
[int(i) for i in input()] or with a given integer [int(i) for i in str(integer)] or split(str(integer))
26th Oct 2018, 6:40 PM
Martin Möhle
Martin Möhle - avatar
+ 2
#The quick and easy way num = 15 digits = [int(i) for i in list(str(num))] print(digits)
26th Oct 2018, 7:46 PM
Shuaib Nuruddin
Shuaib Nuruddin - avatar
+ 2
#assume that you assign 15 to num variable num = 15 num = str(num) lst = [] for i in num: lst.append(int(i)) #if you want to check it print(lst)
27th Oct 2018, 6:22 AM
RizVrisz