Skip to content
Advertisement

How to split an integer into a list of digits?

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:

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

[1, 2, 3, 4, 5]
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement