Is there anyway to convert a series of integers into a list
e.g.
12345
to ['1','2','3','4','5']
Asked
Active
Viewed 5,397 times
-2
-
I mean probably not into *that* list, but yes. – jonrsharpe Aug 23 '16 at 17:19
-
Possible duplicate of these: http://stackoverflow.com/q/1906717/478656; http://stackoverflow.com/q/780390/478656; http://stackoverflow.com/q/13905936/478656; http://stackoverflow.com/q/974952/478656; http://stackoverflow.com/q/5242798/478656; – TessellatingHeckler Aug 23 '16 at 18:02
2 Answers
1
list(str(i))
The reason this works is that str
turns the integer (e.g. 12345) into its string representation ('12345'
), and strings are iterable, thus direct conversion to a list (of component characters) is possible.
In [1]: i = 12345
In [2]: list(str(i))
Out[2]: ['1', '2', '3', '4', '5']

Two-Bit Alchemist
- 17,966
- 6
- 47
- 82
0
Depending on what you mean by "series of integers"
If you mean a big numbers, Lola's solution is find, but the easier one is:
n = 12345
a = [c for c in str(n)]
If you mean "turn array of integers in array of strings"
a = [1, 2, 3, 4, 5, 6]
b = list(map(int, a))

Dmitry Torba
- 3,004
- 1
- 14
- 24
-
`[c for c in str(n)]` <-- if you're not doing anything to each member there's no reason to use a comprehension. Directly cast to list: `list(str(n))`. – Two-Bit Alchemist Aug 23 '16 at 17:58