Open In App

Python – Convert Number to List of Integers

Last Updated : 28 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

We need to split a number into its individual digits and represent them as a list of integers. For instance, the number 12345 can be converted to the list [1, 2, 3, 4, 5]. Let’s discuss several methods to achieve this conversion.

Using map() and str()

Combination of str() and map() is a straightforward and efficient way to convert a number into a list of integers.

Python
# Define the number
n = 12345

# Convert number to list of integers
res = list(map(int, str(n)))

print(res) 

Output
[1, 2, 3, 4, 5]

Explanation:

  • str() function converts the number into a string, allowing iteration over each digit.
  • map() function applies int to each character of the string, converting it back to an integer.
  • Finally, the list() constructor converts the map object into a list.

Let’s explore some more ways and see how we can convert numbers to a list of integers.

Using List Comprehension

List comprehension provides a Pythonic way to convert a number into a list of integers.

Python
# Define the number
n = 12345

# Convert number to list of integers using list comprehension
res = [int(digit) for digit in str(n)]

print(res) 

Output
[1, 2, 3, 4, 5]

Explanation:

  • str() function is used to convert the number into a string.
  • list comprehension iterates over each character of the string, converts it to an integer, and adds it to the list.

Using a While Loop

A while loop can be used to manually extract digits from the number and store them in a list.

Python
# Define the number
n = 12345

# Initialize an empty list
res = []

# Extract digits using a while loop
while n > 0:
    res.append(n % 10)  # Extract the last digit
    n //= 10  # Remove the last digit

# Reverse the list to maintain the original order
res = res[::-1]

print(res) 

Output
[1, 2, 3, 4, 5]

Explanation:

  • While loop iterates until the number becomes 0.
  • Last digit is extracted using the modulo operator % and added to the list.
  • The number is reduced by removing the last digit (num //= 10).
  • The list is reversed at the end to restore the original order of the digits.

Using divmod()

divmod() function can simplify the process of extracting digits manually.

Python
# Define the number
n = 12345

# Initialize an empty list
res = []

# Extract digits using divmod
while n > 0:
    n, digit = divmod(n, 10)  # Extract last digit and update number
    res.append(digit)

# Reverse the list to maintain the original order
res = res[::-1]

print(res) 

Output
[1, 2, 3, 4, 5]

Explanation:

  • divmod() function divides the number by 10, returning both the quotient and remainder.
  • The remainder (last digit) is appended to the list.
  • list is reversed at the end to maintain the correct order of digits.

Using Recursion

Recursion can also be used to break down the number into its digits and store them in a list.

Python
# Define the function to extract digits recursively
def number_to_list(n):
    if n == 0:
        return []
    return number_to_list(n // 10) + [n % 10]

# Define the number
n = 12345

# Convert number to list of integers
res = number_to_list(n)

print(res)

Output
[1, 2, 3, 4, 5]

Explanation:

  • The function keeps dividing the number by 10 to extract digits.
  • The base case stops the recursion when the number reaches 0.
  • The digits are added to the list in the correct order during the recursive return.


Next Article
Practice Tags :

Similar Reads