Open In App

Python | Convert number to list of integers

Last Updated : 21 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate
# conversion of number to list of integers
# using list comprehension
 
# initializing number
num = 2019
 
# printing number
print("The original number is " + str(num))
 
# using list comprehension
# to convert number to list of integers
res = [int(x) for x in str(num)]
 
# printing result
print("The list from number is " + str(res))


Output

The 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




# Python3 code to demonstrate
# conversion of number to list of integers
# using map()
 
# initializing number
num = 2019
 
# printing number
print("The original number is " + str(num))
 
# using map()
# to convert number to list of integers
res = list(map(int, str(num)))
 
# printing result
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)


Output

[2, 0, 1, 9]

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




# Python3 code to demonstrate
# conversion of number to list of integers
# using divmod and a loop
 
# initializing number
num = 2019
 
# printing number
print("The original number is " + str(num))
 
# using divmod and a loop
# to convert number to list of integers
res = []
while num > 0:
    num, remainder = divmod(num, 10)
    res.insert(0,remainder)
 
# printing result
print("The list from number is " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original number is 2019
The list from number is [2, 0, 1, 9]

Method: Using reduce:

Algorithm:
 

  1. Initialize the number num to the value 2019.
  2. Print the original number num.
  3. Convert the number to a list of integers by using the list comprehension:
  4. Convert the number to a string using str() function.
  5. Iterate over each character in the string using a for loop.
  6. Convert each character back to an integer using the int() function.
  7. Append the resulting integer to a new list using the append() method.
  8. Print the resulting list of integers.

Python3




from functools import reduce
 
# initializing number
num = 2019
 
# printing number
print("The original number is " + str(num))
 
# using reduce method to convert number to list of integers
res = reduce(lambda acc, x: acc + [int(x)], str(num), [])
 
# printing result
print("The list from number is " + str(res))
#This code is contributed by Rayudu.


Output

The 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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads