35

How do I write the magic function below?

>>> num = 123
>>> lst = magic(num)
>>>
>>> print lst, type(lst)
[1, 2, 3], <type 'list'>
Paolo Bergantino
  • 466,948
  • 77
  • 516
  • 433
user94774
  • 393
  • 1
  • 3
  • 4

11 Answers11

79

You mean this?

num = 1234
lst = [int(i) for i in str(num)]
Bjorn
  • 64,873
  • 39
  • 133
  • 161
24
a = 123456
b = str(a)
c = []

for digit in b:
    c.append (int(digit))

print c
RedBlueThing
  • 40,938
  • 17
  • 97
  • 122
16

You could do this:

>>> num = 123
>>> lst = map(int, str(num))
>>> lst, type(lst)
([1, 2, 3], <type 'list'>)
John Fouhy
  • 39,311
  • 19
  • 61
  • 77
11
magic = lambda num: map(int, str(num))

then just do

magic(12345) 

or

magic(someInt) #or whatever
jamylak
  • 120,885
  • 29
  • 225
  • 225
Alex
  • 4,166
  • 2
  • 23
  • 28
9
>>> from collections import deque
>>> def magic(num):
        digits = deque()
        while True:
            num,r = divmod(num,10)
            digits.appendleft(r)
            if num == 0:
                break
        return list(digits)

>>> magic(123)
[1, 2, 3]

According to my timings, this solution is considerably faster than the string method (magic2), even for smaller examples.

>>> def magic2(num):
        return [int(i) for i in str(num)]

Timings:

magic

>>> timeit.timeit(setup='from __main__ import magic', stmt='magic(123)')
1.3874572762508706
>>> timeit.timeit(setup='from __main__ import magic', stmt='magic(999999999)')
3.2624468999981673

magic2

>>> timeit.timeit(setup='from __main__ import magic2', stmt='magic2(123)')
3.693756106896217    
>>> timeit.timeit(setup='from __main__ import magic2', stmt='magic2(999999999)')
10.485281719412114
Martijn Pieters
  • 963,270
  • 265
  • 3,804
  • 3,187
jamylak
  • 120,885
  • 29
  • 225
  • 225
  • `map(int, str(n))` from the answer of @Wilbeibi is faster than both (tested on Python3) – Felipe Buccioni May 12 '17 at 04:11
  • @FelipeBuccioni `map` in python 3 will just create a generator, not a `list`. You need `list(map(int, str(n))`. `map(int, str(n))` does pretty much nothing by itself. – jamylak May 25 '17 at 08:07
6

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)]
Boris Gorelik
  • 27,385
  • 36
  • 123
  • 169
3
num = map(int, list(str(num)))
wilbeibi
  • 3,343
  • 4
  • 24
  • 43
3

If it is named as magic, why not just use magic:

def magic(x):
    if x < 10:
        return [x]
    else:
        return magic(x//10) + [x%10]
englealuze
  • 1,393
  • 10
  • 17
1

for python 3.x:

num = 1234
lst = list(map(int, str(num)))
Marko Tankosic
  • 115
  • 1
  • 10
0

You can try this:

def convert_to_list(number):
    return list(map(lambda x: int(x), str(number)))

convert_to_list(1245)
Anthony
  • 873
  • 1
  • 13
  • 23
-1

Just use :

a= str (num)
lst = list(a)
jamylak
  • 120,885
  • 29
  • 225
  • 225
MrDHat
  • 7
  • 1
  • 2