AndyyRi AndyyRi - 1 year ago 73
Python Question

Python - Split user input integer into list, where each entry is 2 digits

So I'm trying to split an arbitrarily long user input integer into a list where each entry is 2 digits, and if the number has an odd amount of integers put the only single digit as the first digit. (Where I will then proceed to put a zero in front of it)

I know that putting user integer input into a list looks like:

userintegerlist = [int(i) for i in str(user_input)]
print userintegerlist

And my input (say it's
) will look like
. But I want it to look like:
. Or if input is
, it will be:

Is this possible? All the code is in Python by the way.

wim wim
Answer Source

You can do it with string methods fairly easily, as other answers have already shown. I direct you to the related grouper recipe in itertools.

I want to mention that it may be more efficient to do it with maths:

>>> n = 45346
>>> output = []
>>> while n:
...     output.append(n % 100)
...     n //= 100
>>> output = output[::-1]
>>> print output
[4, 53, 46]