I have a string 12345678 and I want to convert it into a list [1,2,3,4,5,6,7,8] in python.
I tried this method :
I have a string 12345678 and I want to convert it into a list [1,2,3,4,5,6,7,8] in python.
I tried this method :
You can use map
:
list(map(int, '12345678')) # [1, 2, 3, 4, 5, 6, 7, 8]
Or a list comprehension:
[int(x) for x in '12345678'] # [1, 2, 3, 4, 5, 6, 7, 8]
If you want without loop or map, You can try:
final_=[]
def recursive(string1):
if not string1:
return 0
else:
final_.append(int(string1[0]))
return recursive(string1[1:])
recursive('12345678')
print(final_)
output:
[1, 2, 3, 4, 5, 6, 7, 8]