Python - Convert Number to List of Integers
Last Updated : 28 Jan, 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:
- 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)
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)
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)
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)
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.
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
5 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 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