The Wayback Machine - https://web.archive.org/web/20230309164858/https://www.geeksforgeeks.org/python-convert-number-to-list-of-integers/
Skip to content
Related Articles
Open in App
Not now

Related Articles

Python | Convert number to list of integers

Improve Article
Save Article
  • Difficulty Level : Hard
  • Last Updated : 30 Dec, 2022
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

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]

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]

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!