In Python, you can convert an integer to a list of digits in the following ways:
- Converting Numeric String to List of Characters;
- Reducing to Single Digit;
- Using Regular Expression.
Converting Numeric String to List of Characters
If you are certain that the integer is always going to be positive, then you can simply do the following:
- Convert integer to numeric string;
- Convert numeric string to a list of characters;
- Use list comprehension to convert each list item to an integer.
For example, this can be implemented like so:
def num_to_list(num):
# 1: convert integer to string
# 2: convert numeric string to list of chars
# 3: convert each numeric string character to integer
return [int(char) for char in str(num)]
print(num_to_list(12345)) # [1, 2, 3, 4, 5]
print(num_to_list(0)) # [0]
However, if the integer can also be negative, then the following are some ways in which you can handle the negative sign (-) when converting an integer to a list of digits:
- Make the Most Significant Digit Negative;
- Add the Minus Sign as a String;
- Add a Leading Digit to Represent the Sign.
Make the Most Significant Digit Negative:
Making the most significant digit negative means that in the resulting list, if the original number is negative, the first digit will have the negative sign. This means that:
# 12345 becomes [1, 2, 3, 4, 5] # -12345 becomes [-1, 2, 3, 4, 5]
For example, this can be implemented like so:
def num_to_list(num):
# 1: convert integer in absolute form to string
# 2: convert numeric string to list of chars
# 3: convert each numeric string character to integer
digits = [int(char) for char in str(abs(num))]
# 4: make first digit negative if original number is negative
digits[0] = -digits[0] if num < 0 else digits[0]
return digits
print(num_to_list(12345)) # [1, 2, 3, 4, 5]
print(num_to_list(-12345)) # [-1, 2, 3, 4, 5]
print(num_to_list(0)) # [0]
print(num_to_list(-0)) # [0]
Add the Minus Sign as a String:
Adding the minus sign (-) as a string means that in the resulting list, if the original number is negative, the first element of the list will have the minus sign as a string:
# 12345 becomes [1, 2, 3, 4, 5] # -12345 becomes ['-', 1, 2, 3, 4, 5]
For example, this can be implemented like so:
def num_to_list(num):
# 1: convert integer in absolute form to string
# 2: convert numeric string to list of chars
# 3: convert each numeric string character to integer
digits = [int(char) for char in str(abs(num))]
# 4: add minus sign if original number is negative
if num < 0:
digits.insert(0, '-')
return digits
print(num_to_list(12345)) # [1, 2, 3, 4, 5]
print(num_to_list(-12345)) # ['-', 1, 2, 3, 4, 5]
print(num_to_list(0)) # [0]
print(num_to_list(-0)) # [0]
Add a Leading Digit to Represent the Sign
Adding a leading digit to represent the sign means that in the resulting list, the first element can be used to represent the sign (i.e. 0 for positive integers and 1 for negative integers):
# 12345 becomes [0, 1, 2, 3, 4, 5] # -12345 becomes [1, 1, 2, 3, 4, 5]
For example, this can be implemented like so:
def num_to_list(num):
# 1: convert integer in absolute form to string
# 2: convert numeric string to list of chars
# 3: convert each numeric string character to integer
digits = [int(char) for char in str(abs(num))]
# 4: add 0 or 1 as first element in list
digits.insert(0, 1 if num < 0 else 0)
return digits
print(num_to_list(12345)) # [0, 1, 2, 3, 4, 5]
print(num_to_list(-12345)) # [1, 1, 2, 3, 4, 5]
print(num_to_list(0)) # [0, 0]
print(num_to_list(-0)) # [0, 0]
Reducing to Single Digit
If you are certain that the integer is always going to be positive, then you can simply do the following:
- Create a loop;
- Add the last digit (i.e. remainder of
x % 10) as the first element to a new list; - Remove the last digit from the integer in each iteration till there are no digits left.
For example, this can be implemented like so:
def num_to_list(num):
digits = []
# 1: create a loop
while True:
# 2: add last digit as first element in new list
digits.insert(0, num % 10)
# 3: remove last digit from integer
num //= 10
# ...till there are no digits left
if num == 0:
break
return digits
print(num_to_list(12345)) # [1, 2, 3, 4, 5]
print(num_to_list(0)) # [0]
However, if the integer can also be negative, then you can handle the negative sign (-) in the following ways:
- Make the Most Significant Digit Negative;
- Add the Minus Sign as a String;
- Add a Leading Digit to Represent the Sign.
Make the Most Significant Digit Negative:
Making the most significant digit negative means that in the resulting list, if the original number is negative, the first digit will have the negative sign. This means that:
# 12345 becomes [1, 2, 3, 4, 5] # -12345 becomes [-1, 2, 3, 4, 5]
For example, this can be implemented like so:
def num_to_list(num):
# 1: convert integer to absolute form
abs_num = abs(num)
digits = []
# 2: create a loop
while True:
# 3: add last digit as first element in new list
digits.insert(0, abs_num % 10)
# 4: remove last digit from integer
abs_num //= 10
# ...till there are no digits left
if abs_num == 0:
break
# 5: make first digit negative if original number is negative
if num < 0:
digits[0] = -digits[0]
return digits
print(num_to_list(12345)) # [1, 2, 3, 4, 5]
print(num_to_list(-12345)) # [-1, 2, 3, 4, 5]
print(num_to_list(0)) # [0]
print(num_to_list(-0)) # [0]
Add the Minus Sign as a String:
Adding the minus sign (-) as a string means that in the resulting list, if the original number is negative, the first element of the list will have the minus sign as a string:
# 12345 becomes [1, 2, 3, 4, 5] # -12345 becomes ['-', 1, 2, 3, 4, 5]
For example, this can be implemented like so:
def num_to_list(num):
# 1: convert integer to absolute form
abs_num = abs(num)
digits = []
# 2: create a loop
while True:
# 3: add last digit as first element in new list
digits.insert(0, abs_num % 10)
# 4: remove last digit from integer
abs_num //= 10
# ...till there are no digits left
if abs_num == 0:
break
# 5: add '-' to front of list
if num < 0:
digits.insert(0, '-')
return digits
print(num_to_list(12345)) # [1, 2, 3, 4, 5]
print(num_to_list(-12345)) # ['-', 1, 2, 3, 4, 5]
print(num_to_list(0)) # [0, 0]
print(num_to_list(-0)) # [0, 0]
Add a Leading Digit to Represent the Sign:
Adding a leading digit to represent the sign means that in the resulting list, the first element can be used to represent the sign (i.e. 0 for positive integers and 1 for negative integers):
# 12345 becomes [0, 1, 2, 3, 4, 5] # -12345 becomes [1, 1, 2, 3, 4, 5]
For example, this can be implemented like so:
def num_to_list(num):
# 1: convert integer to absolute form
abs_num = abs(num)
digits = []
# 2: create a loop
while True:
# 3: add last digit as first element in new list
digits.insert(0, abs_num % 10)
# 4: remove last digit from integer
abs_num //= 10
# ...till there are no digits left
if abs_num == 0:
break
# 5: add 0 or 1 as first element in list
digits.insert(0, 1 if (num < 0) else 0)
return digits
print(num_to_list(12345)) # [0, 1, 2, 3, 4, 5]
print(num_to_list(-12345)) # [1, 1, 2, 3, 4, 5]
print(num_to_list(0)) # [0, 0]
print(num_to_list(-0)) # [0, 0]
Using Regular Expression
If you are certain that the integer is always going to be positive, then you can simply do the following:
- Use
re.findall()with a regular expression to convert numeric string to a list of numeric string characters; - Use list comprehension to convert each list item to an integer.
For example, this can be implemented like so:
import re
def num_to_list(num):
# 1: use regex to match numbers
matches = re.findall(r'\d', str(num))
# 2: convert each item to an integer
return [int(match) for match in matches]
print(num_to_list(12345)) # [1, 2, 3, 4, 5]
print(num_to_list(0)) # [0]
However, if the integer can also be negative, then you can handle the negative sign (-) in the following ways:
- Make the Most Significant Digit Negative;
- Add the Minus Sign as a String;
- Add a Leading Digit to Represent the Sign.
Make the Most Significant Digit Negative:
Making the most significant digit negative means that in the resulting list, if the original number is negative, the first digit will have the negative sign:
# 12345 becomes [1, 2, 3, 4, 5] # -12345 becomes [-1, 2, 3, 4, 5]
For example, this can be implemented like so:
import re
def num_to_list(num):
# 1: use regex to match numbers
matches = re.findall(r'-?\d', str(num))
# 2: convert each item to an integer
return [int(match) for match in matches]
print(num_to_list(12345)) # [1, 2, 3, 4, 5]
print(num_to_list(-12345)) # [-1, 2, 3, 4, 5]
print(num_to_list(0)) # [0]
print(num_to_list(-0)) # [0]
Add the Minus Sign as a String:
Adding the minus sign (-) as a string means that in the resulting list, if the original number is negative, the first element of the list will have the minus sign as a string:
# 12345 becomes [1, 2, 3, 4, 5] # -12345 becomes ['-', 1, 2, 3, 4, 5]
For example, this can be implemented like so:
import re
def num_to_list(num):
# 1: use regex to match numbers
matches = re.findall(r'\d', str(num))
# 2: convert each item to an integer
digits = [int(match) for match in matches]
# 3: add '-' to the front of list
if num < 0:
digits.insert(0, '-')
return digits
print(num_to_list(12345)) # [1, 2, 3, 4, 5]
print(num_to_list(-12345)) # ['-', 1, 2, 3, 4, 5]
print(num_to_list(0)) # [0]
print(num_to_list(-0)) # [0]
Add a Leading Digit to Represent the Sign:
Adding a leading digit to represent the sign means that in the resulting list, the first element can be used to represent the sign (i.e. 0 for positive integers and 1 for negative integers):
# 12345 becomes [0, 1, 2, 3, 4, 5] # -12345 becomes [1, 1, 2, 3, 4, 5]
For example, this can be implemented like so:
import re
def num_to_list(num):
# 1: use regex to match numbers
matches = re.findall(r'\d', str(num))
# 2: convert each item to an integer
digits = [int(match) for match in matches]
# 3: add 0 or 1 as first element in list
digits.insert(0, 1 if (num < 0) else 0)
return digits
print(num_to_list(12345)) # [0, 1, 2, 3, 4, 5]
print(num_to_list(-12345)) # [1, 1, 2, 3, 4, 5]
print(num_to_list(0)) # [0, 0]
print(num_to_list(-0)) # [0, 0]
This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.