-5

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 tried this method

2

2 Answers 2

3

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]
Sign up to request clarification or add additional context in comments.

1 Comment

map(int, '12345678') is enough.
0

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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.