I take a string of integers as input and there are no spaces or any kind of separator:
12345
Now I want this string to converted into a list of individual digits
[1,2,3,4,5]
I've tried both
numlist = map(int,input().split(""))
and
numlist = map(int,input().split(""))
Both of them give me Empty Separator error. Is there any other function to perform this task?
Method#1: Using split() method If a delimiter is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace.
Use list_comprehension.
>>> s = "12345"
>>> [int(i) for i in s]
[1, 2, 3, 4, 5]
You don't need to use split here:
>>> a = "12345"
>>> map(int, a)
[1, 2, 3, 4, 5]
Strings are Iterable too
For python 3x:
list(map(int, a))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With