View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
The interconversion of data types is a problem that is quite common in programming. Sometimes we need to convert a single number to list of integers and we don’t wish to spend several lines of code doing it. Hence having ways to perform this task using shorthands comes in handy. Let’s discuss ways in which this can be performed.
Method #1: Using list comprehension
Itcan be used as a shorthand for the longer format of the naive method. In this method, we convert the number to a string and then extract each character and re-convert it to an integer.
Python3
num
=
2019
print
[
"The original number is "
+
str
[num]]
res
=
[
int
[x]
for
x
in
str
[num]]
print
[
"The list from number is "
+
str
[res]]
Output
The original number is 2019 The list from number is [2, 0, 1, 9]
Method #2: Using map[] map function can be used to perform the following task converting each of the string converted numbers to the desired integer value to be reconverted to the list format.
Python3
num
=
2019
print
[
"The original number is "
+
str
[num]]
res
=
list
[
map
[
int
,
str
[num]]]
print
[
"The list from number is "
+
str
[res]]
Output
The original number is 2019 The list from number is [2, 0, 1, 9]
Method #3: Using enumerate function
Python3
n
=
2019
res
=
[
int
[x]
for
a,x
in
enumerate
[
str
[n]]]
print
[res]
There are already great methods already mentioned on this page, however it does seem a little obscure as to which to use. So I have added some mesurements so you can more easily decide for yourself:
A large number has been used [for overhead] 1111111111111122222222222222222333333333333333333333
Using map[int, str[num]]
:
import timeit
def method[]:
num = 1111111111111122222222222222222333333333333333333333
return map[int, str[num]]
print[timeit.timeit["method[]", setup="from __main__ import method", number=10000]
Output: 0.018631496999999997
Using list comprehension:
import timeit
def method[]:
num = 1111111111111122222222222222222333333333333333333333
return [int[x] for x in str[num]]
print[timeit.timeit["method[]", setup="from __main__ import method", number=10000]]
Output:
0.28403817900000006
Code taken from this answer
The results show that the first method involving inbuilt methods is much faster than list comprehension.
The "mathematical way":
import timeit
def method[]:
q = 1111111111111122222222222222222333333333333333333333
ret = []
while q != 0:
q, r = divmod[q, 10] # Divide by 10, see the remainder
ret.insert[0, r] # The remainder is the first to the right digit
return ret
print[timeit.timeit["method[]", setup="from __main__ import method", number=10000]]
Output: 0.38133582499999996
Code taken from this answer
The list[str[123]]
method [does not provide the right output]:
import timeit
def method[]:
return list[str[1111111111111122222222222222222333333333333333333333]]
print[timeit.timeit["method[]", setup="from __main__ import method", number=10000]]
Output: 0.028560138000000013
Code taken from this answer
The answer by Duberly González Molinari:
import timeit
def method[]:
n = 1111111111111122222222222222222333333333333333333333
l = []
while n != 0:
l = [n % 10] + l
n = n // 10
return l
print[timeit.timeit["method[]", setup="from __main__ import method", number=10000]]
Output: 0.37039988200000007
Code taken from this answer
Remarks:
In all cases the map[int, str[num]]
is the fastest method [and is therefore probably the best method to use]. List comprehension is the second fastest [but the method using map[int, str[num]]
is probably the most
desirable of the two.
Those that reinvent the wheel are interesting but are probably not so desirable in real use.