Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python | Convert Stream of numbers to list
Next article icon

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.

Table of Content

  • Using List Comprehension
  • Using a While Loop
  • Using divmod()
  • Using Recursion

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
Python | Convert Stream of numbers to list
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
Practice Tags :
  • python

Similar Reads

  • Python | Convert list of numerical string to list of Integers
    Many times, the data we handle might not be in the desired form for any application and has to go through the stage of preprocessing. One such kind of form can be a number in the form of a string that too is a list in the list and we need to segregate it into digit-separated integers. Let's discuss
    6 min read
  • Python | Convert Stream of numbers to list
    Sometimes, we can be stuck with a problem in which we are given a stream of space separated numbers with a goal to convert them into a list of numbers. This type of problem can occur in common day-day programming or competitive programming while taking inputs. Let's discuss certain ways in which thi
    5 min read
  • Python | Convert numeric String to integers in mixed List
    Sometimes, while working with data, we can have a problem in which we receive mixed data and need to convert the integer elements in form of strings to integers. This kind of operation might be required in data preprocessing step. Let's discuss certain ways in which this task can be performed. Metho
    11 min read
  • Python - Convert Delimiter separated list to Number
    Given a String with delimiter separated numbers, concatenate to form integer after removing delimiter. Input : test_str = "1@6@7@8", delim = '@' Output : 1678 Explanation : Joined elements after removing delim "@"Input : test_str = "1!6!7!8", delim = '!' Output : 1678 Explanation : Joined elements a
    6 min read
  • Python - Convert List of Integers to a List of Strings
    We are given a list of integers and our task is to convert each integer into its string representation. For example, if we have a list like [1, 2, 3] then the output should be ['1', '2', '3']. In Python, there are multiple ways to do this efficiently, some of them are: using functions like map(), re
    3 min read
  • Python | Convert Integral list to tuple list
    Sometimes, while working with data, we can have a problem in which we need to perform type of interconversions of data. There can be a problem in which we may need to convert integral list elements to single element tuples. Let's discuss certain ways in which this task can be performed. Method #1 :
    3 min read
  • Python | Convert list of tuples into list
    In Python we often need to convert a list of tuples into a flat list, especially when we work with datasets or nested structures. In this article, we will explore various methods to Convert a list of tuples into a list. Using itertools.chain() itertools.chain() is the most efficient way to flatten a
    3 min read
  • Python - Binary list to integer
    A binary list represents binary digits (0s and 1s) as individual elements of a list. This article will explore various methods to convert a binary list into an integer. Using int() with String ConversionThis is the most efficient method. By joining the binary list into a string and using the built-i
    3 min read
  • Python | Convert list into list of lists
    Given a list of strings, write a Python program to convert each element of the given list into a sublist. Thus, converting the whole list into a list of lists. Examples: Input : ['alice', 'bob', 'cara'] Output : [['alice'], ['bob'], ['cara']] Input : [101, 202, 303, 404, 505] Output : [[101], [202],
    5 min read
  • Python - Convert a list into tuple of lists
    When working with data structures in Python, there are times when we need to convert a list into a tuple of smaller lists. For example, given a list [1, 2, 3, 4, 5, 6], we may want to split it into a tuple of two lists like ([1, 2, 3], [4, 5, 6]). We will explore different methods to achieve this co
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences