How to create a list of a number?

Question:

I am new to python and i was wondering how i could create a list of numbers in python as such

number = 123456
list_to_be_created = [1,2,3,4,5,6]

Thank you in advance

Asked By: Harsh

||

Answers:

>>> list(range(1, 7))
[1, 2, 3, 4, 5, 6]

If you literally have the number 123456 (i.e. the int value for one hundred twenty three thousand four hundred fifty six) and you want to turn it into a list of its digits, you might do:

>>> number = 123456
>>> list_to_be_created = list(map(int, str(number)))
>>> list_to_be_created
[1, 2, 3, 4, 5, 6]
Answered By: Samwise

You can try with a list comprehension:

number = 123456
print([int(x) for x in str(number)])

Returns:

[1, 2, 3, 4, 5, 6]
Answered By: Celius Stingher
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.