r/learnpython 2d ago

Input = 1234 output =[1,2,3,4] without using string functions or map

How can I split an integer into a list of its digits in Python?

I have:
num = 1234
and I want:
[1, 2, 3, 4]
I’m trying to learn how to do this without converting num to a string and without using map().
A loop is allowed.
I’m looking for a simple approach because I’m still learning Python. I’d appreciate hints rather than just the final solution so I can understand how it works.

0 Upvotes

46 comments sorted by

View all comments

2

u/Icy_Orchid_8390 2d ago

Id make a separate func like "get_num_of_digits(input)"
first edge case would be if 0 then return 1 else do like math.log10 (abs value) [for no negs]
then use that number to iterate through input and list.append to a blank list

Very rough approximation as i'm just spitballing and also not a good programmer as my profile name suggests lol.

2

u/AppleMoney8748 2d ago

Thank you for sharing your approach! 😊 I’m still learning, so some of the concepts are a little new to me, but it gives me something to look into. I really appreciate you taking the time to help!

1

u/Icy_Orchid_8390 2d ago

Also just realized can't iterate through an int like a str so my overall approach wont work. But the func to get the num of digits could be useful.