Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
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 :
"1,2,3,4"
You can use map:
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]
Add a comment
map(int, '12345678')
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]
Required, but never shown
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.
Explore related questions
See similar questions with these tags.
"1,2,3,4"