How to convert an int to a list in Python

Learn how to convert an integer to a list in Python. Explore various methods, tips, real-world applications, and common error debugging.

How to convert an int to a list in Python
Published on: 
Tue
Apr 21, 2026
Updated on: 
Tue
Apr 21, 2026
The Replit Team

You often need to convert an integer into a list of its digits in Python. The language lacks a direct function for this, but a few simple steps will get it done.

In this article, we'll explore several techniques to handle this conversion. You'll find practical tips, see real-world applications, and get common troubleshooting advice to help you select the right approach.

Using str() and list comprehension

number = 12345
digit_list = [int(digit) for digit in str(number)]
print(digit_list)--OUTPUT--[1, 2, 3, 4, 5]

This popular one-liner is both readable and efficient. It works by first converting the integer into a string with the str() function. Since strings are iterable in Python, a list comprehension can then process each character.

For each character in the string, the int() function converts it back into an integer. All the resulting integers are then collected into a new list. This technique elegantly combines type casting and iteration into a single, expressive statement.

Traditional conversion methods

Beyond the popular one-liner, you can also use more traditional methods involving while loops, recursion, or the map() function to achieve the same goal.

Using a while loop to extract digits

number = 12345
digits = []
while number > 0:
digits.insert(0, number % 10)
number //= 10
print(digits)--OUTPUT--[1, 2, 3, 4, 5]

This approach mathematically deconstructs the number. A while loop runs as long as the number is greater than zero, processing one digit at a time from right to left.

  • The modulo operator (% 10) isolates the last digit of the number.
  • The insert(0, ...) method adds this digit to the beginning of the list, which preserves the original order.
  • Floor division (//= 10) then removes the last digit from the number, preparing it for the next loop cycle.

Using recursion to build the list

def int_to_list(num):
if num < 10:
return [num]
return int_to_list(num // 10) + [num % 10]

print(int_to_list(12345))--OUTPUT--[1, 2, 3, 4, 5]

Recursion offers an elegant, functional way to solve this problem. The int_to_list function repeatedly calls itself, breaking the number down piece by piece until it can't be broken down any further.

  • The function's base case is when the number is less than 10. It simply returns a list containing that single digit, which stops the recursion.
  • Otherwise, it calls itself with the remaining digits (using num // 10) and appends the last digit (num % 10) to the resulting list.

This process unwinds to construct the full list of digits in the correct order.

Using the map() function with string conversion

number = 12345
digit_list = list(map(int, str(number)))
print(digit_list)--OUTPUT--[1, 2, 3, 4, 5]

This method provides a functional alternative to list comprehensions. The map() function is designed to apply another function to every item in an iterable, like a string or list. It's a concise way to handle transformations.

  • First, str(number) converts the integer into a string.
  • The map() function then applies the int() function to each character in that string.
  • Finally, list() is used to convert the resulting map object—which is an iterator—into a list of digits.

Advanced techniques

For those who enjoy more specialized or functional programming styles, Python offers even more creative ways to deconstruct an integer into its digits.

Using divmod() for digit extraction

number = 12345
digits = []
while number:
number, digit = divmod(number, 10)
digits.insert(0, digit)
print(digits)--OUTPUT--[1, 2, 3, 4, 5]

This method is a more efficient spin on the traditional while loop. The built-in divmod() function is the key—it combines floor division and the modulo operation into a single, optimized step. In each iteration, it processes the number from right to left.

  • The function call divmod(number, 10) returns a tuple with the quotient and remainder, which you unpack into number and digit.
  • Using insert(0, digit) then adds the extracted digit to the front of the list, which keeps the digits in their original order.

Using functools.reduce() for a functional approach

from functools import reduce

number = 12345
digits = reduce(lambda acc, digit: acc + [int(digit)], str(number), [])
print(digits)--OUTPUT--[1, 2, 3, 4, 5]

The reduce() function offers a purely functional way to build the list. It works by applying a lambda function cumulatively to each character of the string, starting with an empty list [] as the initial value.

  • The lambda function takes two arguments: an accumulator (acc), which is the list you're building, and the current digit from the string.
  • In each step, it appends the integer version of the current digit to the accumulator.
  • This process repeats until all characters are processed, reducing the string to a single list of digits.

Using list operations with negative indexing

number = 12345
digits = []
for i in range(len(str(number))):
digits.append(int(str(number)[i]))
print(digits)--OUTPUT--[1, 2, 3, 4, 5]

This approach uses a standard for loop to iterate through the number's string representation by index. It's a more explicit method, breaking the process into clear, sequential steps which can be easier to follow than a one-liner.

  • The loop generates indices from 0 up to the string's length using range(len(str(number))).
  • In each iteration, it accesses the character at the current index, converts it to an integer, and uses append() to add it to the list.

Move faster with Replit

Replit is an AI-powered development platform where all Python dependencies come pre-installed, so you can skip setup and start coding instantly. There's no need to configure environments or manage packages, letting you focus on what you want to build.

While mastering individual techniques is essential, Agent 4 helps you move from piecing them together to building complete applications. Instead of just writing snippets, you can describe the app you want to build, and the Agent will take it from an idea to a working product. For example, you could build:

  • A Luhn algorithm validator that splits a credit card number into its digits to check its validity.
  • A digital root calculator that repeatedly sums the digits of an input number until a single-digit result is achieved.
  • A dashboard widget that breaks down a large statistic into individual digits for an animated display.

Simply describe your app, and Replit will write the code, test it, and fix issues automatically, all within your browser.

Common errors and challenges

When converting integers to lists, you might run into a few common pitfalls with edge cases like negative numbers and zero.

Handling negative numbers when using str() conversion

If you use the str() method on a negative number, the resulting string will include a minus sign. For example, -123 becomes '-123'. Attempting to convert the '-' character to an integer with int() will raise a ValueError, breaking your code.

To avoid this, you can handle the sign separately. A simple solution is to use the abs() function on the number before converting it to a string. This ensures you're only working with the digits themselves, sidestepping the error entirely.

Handling zero as an edge case with the while loop method

The mathematical approach using a while number > 0 loop works great for positive integers, but it fails for zero. If the input number is 0, the loop's condition is immediately false, and it never runs. You'll end up with an empty list instead of the expected [0].

The fix is to add a special check for this edge case. A simple if statement at the beginning of your function to see if the number is zero can handle it correctly before the loop begins.

Forgetting to convert string digits to integers

A frequent oversight, especially when using string conversion, is forgetting to turn the string characters back into integers. If you skip the int() conversion, you'll get a list of strings, like ['1', '2', '3'], instead of a list of numbers, [1, 2, 3].

This might not cause an immediate error, but it will lead to unexpected behavior if you try to perform mathematical operations on the list's elements. Always ensure each character is cast back to an integer to get a list you can actually work with numerically.

Handling negative numbers when using str() conversion

While the list comprehension one-liner is elegant, it hits a snag with negative numbers. The minus sign included in the str() conversion can't be processed back into a number. The code below shows exactly what happens when you attempt this.

number = -12345
digits = [int(digit) for digit in str(number)]
print(digits)

The list comprehension fails on the first character. The int() function can't convert the minus sign into a number, which triggers a ValueError. The code below shows how to properly handle negative inputs to avoid this issue.

number = -12345
digits = [int(digit) for digit in str(abs(number))]
print(digits)

By wrapping the number in the abs() function, you get its absolute value before converting it to a string. This strips away the negative sign, ensuring the list comprehension only has to process digits. This simple fix prevents a ValueError and is a good practice to adopt whenever your input might include negative numbers, making your code more robust.

Handling zero as an edge case with the while loop method

The mathematical approach using a while loop is effective for positive integers, but it runs into trouble with zero. Since the loop's condition is while number > 0, an input of 0 prevents it from ever running. The code below shows this problem in action.

number = 0
digits = []
while number > 0:
digits.insert(0, number % 10)
number //= 10
print(digits)

The loop condition while number > 0 is false from the start, so the code block is skipped entirely. This leaves you with an empty list instead of [0]. The code below shows how to address this.

number = 0
if number == 0:
digits = [0]
else:
digits = []
while number > 0:
digits.insert(0, number % 10)
number //= 10
print(digits)

A simple if statement solves the problem. By checking if number == 0 before the loop, you can handle this edge case directly and return [0]. The original while loop logic then goes into an else block to process all positive integers. It's a small addition that makes your mathematical digit extraction robust for any non-negative number. Keep this in mind whenever your logic depends on a number being greater than zero.

Forgetting to convert string digits to integers

It's easy to overlook the final step of converting string characters back into integers. When you skip the int() function, you end up with a list of characters instead of numbers, which can't be used for math. The code below demonstrates this common oversight.

number = 12345
digits = list(str(number))
print(digits)

The list() constructor simply unpacks the string into individual characters. Because each element is text instead of a number, any attempt at arithmetic will fail. The corrected code below shows how to ensure each digit is an integer.

number = 12345
digits = [int(digit) for digit in str(number)]
print(digits)

The corrected code uses a list comprehension to ensure each digit is an integer. By applying the int() function to each character in the string, you get a list of numbers ready for mathematical operations. This step is crucial. Without it, you'd have a list of strings like ['1', '2', '3'], which would cause errors. Always double-check your output's data type after performing type conversions to avoid unexpected behavior.

Real-world applications

Handling these conversions correctly is the first step toward solving classic algorithmic problems that rely on digit-by-digit analysis.

Checking if a number is a palindrome using digits

Once a number is converted into a list of its digits, you can check if it's a palindrome by simply comparing the list to its reversed version.

def is_palindrome(number):
digits = [int(digit) for digit in str(number)]
return digits == digits[::-1]

print(is_palindrome(12321))
print(is_palindrome(12345))

The is_palindrome function determines if a number reads the same forwards and backward. After converting the number into a list of digits, it performs a simple but powerful check.

  • It uses slice notation, digits[::-1], to create a reversed copy of the list in memory.
  • The equality operator == then compares the original digits list with the reversed one.

If every element matches in the same order, the lists are identical, and the function returns True. This confirms the number is a palindrome.

Finding Armstrong numbers using digit powers

This technique also lets you identify Armstrong numbers, which are equal to the sum of their own digits each raised to the power of the total number of digits.

def is_armstrong(number):
digits = [int(digit) for digit in str(number)]
power = len(digits)
armstrong_sum = sum(digit ** power for digit in digits)
return armstrong_sum == number

print(is_armstrong(153))
print(is_armstrong(370))

The is_armstrong function works by deconstructing the input number. First, it converts the number into a list of individual digits. The length of this list gives you the power, which is the total count of digits.

  • It then uses a generator expression to iterate through the digits.
  • Each digit is raised to the calculated power, and the sum() function totals the results.

The function's final step is a boolean comparison. It checks if this calculated sum matches the original number, returning True or False.

Get started with Replit

Put these techniques into practice by building a real tool. Describe what you want to Replit Agent, like “build a Luhn algorithm validator” or “create a digital root calculator from an input number.”

The Agent writes the code, tests for errors, and deploys the app for you. Start building with Replit.

Get started free

Create and deploy websites, automations, internal tools, data pipelines and more in any programming language without setup, downloads or extra tools. All in a single cloud workspace with AI built in.

Get started free

Create and deploy websites, automations, internal tools, data pipelines and more in any programming language without setup, downloads or extra tools. All in a single cloud workspace with AI built in.