A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/python-convert-number-to-list-of-integers/ below:

Python - Convert Number to List of Integers

Python - Convert Number to List of Integers

Last Updated : 11 Jul, 2025

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) 

Explanation:

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) 

Explanation:

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) 

Explanation:

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) 

Explanation:

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)

Explanation:


Python program to convert number to List of Integers


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4