Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Split an Integer into a List of Digits in Python

  • 22 Aug, 2025
  • Com 0
How to Split an Integer into a List of Digits in Python

The most robust and Pythonic way to split an integer into a list is to use a combination of str(), int(), and list comprehension.

Splitting an integer into a list of digits in Python

The first step is to convert an input number into a string using the str() function, which makes it iterable. Then, use a list comprehension to iterate over each character and create a new list from it. When creating a new list, ensure that each character is converted back to an integer.

Here is the code:

new_int = 3456789

print(new_int)

# Output: 3456789

# Using list comprehension to convert integer to list of digits
digits = [int(i) for i in str(new_int)]

print(digits)

# Output: [3, 4, 5, 6, 7, 8, 9]

The time complexity is O(d), where d is the number of digits (log10(num) + 1).

What if the input is a negative number?

What if the input is a negative number_

If you are working with a negative value, the best approach is to either take it as an absolute value, ignore the sign, or raise an error, depending on your requirements.

To calculate the absolute value of an input, always use the built-in abs() function.

def converting_digits_to_list(num):
    num = abs(num)  # Handle negative
    return [int(digit) for digit in str(num)]


print(converting_digits_to_list(-1921))

# Output: [1, 9, 2, 1]

Even if the number is negative, we still obtain a list of positive integers.

Non-Integer Inputs

If you pass a string or data other than a numeric value, it will throw a TypeError. To prevent it, we can use the built-in isintstance() method to check the input value’s type and create a condition based on it.

def converting_digits_to_list(num):
    if not isinstance(num, int):
        print("Input must be an integer")
    else:
        num = abs(num)  # Handle negative
        print([int(digit) for digit in str(num)])


converting_digits_to_list("Krunal")

# Output: Input must be an integer

Using map() with list()

First, you convert the integer to a string using the str() method and then use the map() function with the help of the int() function to iterate over each character in the string “3456789” and convert each character back into an integer.

Finally, wraps the map object in a list(), transforming it into a proper Python list.

Using map() with list() methods

 

new_int = 3456789

print(new_int)

# Output: 3456789

digits = list(map(int, str(new_int)))

print(digits)

# Output: [3, 4, 5, 6, 7, 8, 9]

That’s all!

Post Views: 18
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

Removing the First Character From String in Python
JavaScript Array find() Method

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend