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
It can 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))
|
OutputThe original number is 2019
The list from number is [2, 0, 1, 9]
Time Complexity: O(n), where n is the length of the list test_list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
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))
|
OutputThe 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)
|
Method: Using lambda function
Python3
n = 2019
x = list ( filter ( lambda i:(i), str ( 2019 )))
print (x)
|
Output['2', '0', '1', '9']
Method : Using divmod
One additional approach to convert a number to a list of integers is to use the divmod function to repeatedly divide the number by 10 and get the remainder. This can be done in a loop until the number becomes 0. The remainders can be added to a list, which can then be reversed to get the list of integers in the correct order.
Here is an example of how this can be done:
Python3
num = 2019
print ( "The original number is " + str (num))
res = []
while num > 0 :
num, remainder = divmod (num, 10 )
res.insert( 0 ,remainder)
print ( "The list from number is " + str (res))
|
OutputThe original number is 2019
The list from number is [2, 0, 1, 9]
Method: Using reduce:
Algorithm:
- Initialize the number num to the value 2019.
- Print the original number num.
- Convert the number to a list of integers by using the list comprehension:
- Convert the number to a string using str() function.
- Iterate over each character in the string using a for loop.
- Convert each character back to an integer using the int() function.
- Append the resulting integer to a new list using the append() method.
- Print the resulting list of integers.
Python3
from functools import reduce
num = 2019
print ( "The original number is " + str (num))
res = reduce ( lambda acc, x: acc + [ int (x)], str (num), [])
print ( "The list from number is " + str (res))
|
OutputThe original number is 2019
The list from number is [2, 0, 1, 9]
Time complexity:
The time complexity of this code is O(n), where n is the number of digits in the input number. This is because the list comprehension and for loop both iterate over each digit once, and the int() function takes constant time to convert a single digit to an integer. Therefore, the time complexity grows linearly with the number of digits.
Space complexity:
The space complexity of this code is also O(n), where n is the number of digits in the input number. This is because the list of integers created by the list comprehension will have one element for each digit in the input number, and each element takes up constant space. Therefore, the space used by the resulting list grows linearly with the number of digits in the input number.