How do I write the magic
function below?
>>> num = 123
>>> lst = magic(num)
>>>
>>> print lst, type(lst)
[1, 2, 3], <type 'list'>
How do I write the magic
function below?
>>> num = 123
>>> lst = magic(num)
>>>
>>> print lst, type(lst)
[1, 2, 3], <type 'list'>
You could do this:
>>> num = 123
>>> lst = map(int, str(num))
>>> lst, type(lst)
([1, 2, 3], <type 'list'>)
a = 123456
b = str(a)
c = []
for digit in b:
c.append (int(digit))
print c
Don't use the word list as variable name! It is a name of python built in data type.
Also, please clarify your question. If you are looking for a way to create a one-member list, do the following:
a = 123
my_list = [a]
and "pythonizing" Cannonade's answer:
a = 123
my_list = [int(d) for d in str(a)]
magic = lambda num: map(int, str(num))
then just do magic(12345) or magic(someInt) or whatever