Open In App

Python – Convert Float to digit list

Last Updated : 24 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

We are given a floating-point number and our task is to convert it into a list of its individual digits, ignoring the decimal point. For example, if the input is 45.67, the output should be [4, 5, 6, 7].

Using string manipulation

In this method, the number is converted to a string and then each character is checked to see if it is a digit before being added to the result list.

Python
N = 6.456

# Convert Float to digit list using string manipulation
s = str(N)
res = []

for c in s:
    if c.isdigit():
        res.append(int(c))

print("List of floating numbers is : " + str(res))

Output
List of floating numbers is : [6, 4, 5, 6]

Explanation: str(N) converts 6.456 into ‘6.456’. The loop iterates through each character in the string and isdigit() filters out the decimal point. Valid digits are converted to integers and appended to res thus resulting in [6, 4, 5, 6].

Let’s discuss some other methods of doing the same task.

Using brute force

Convert the float to a string and iterate through its characters. Check if each character is a digit and append it as an integer to a list, effectively extracting all numeric digits while ignoring the decimal point.

Python
N = 6.456

# Convert float to digit list
x = str(N)

res = []
digs = "0123456789"

for i in x:
    if i in digs:
        res.append(int(i))

print("List of floating numbers is : " + str(res))

Output
List of floating numbers is : [6, 4, 5, 6]

Explanation: str(N) converts 6.456 to “6.456”. The loop filters out digits using if i in digs, converts them to integers and appends them to res.

Using list comprehension + isdigit()

We can also achieve the same result with the combination of list comprehension and isdigit() funtion. First convert the float to a string and then use list comprehension with isdigit() to filter and extract only numeric characters, converting them into integers. 

Python
N = 6.456

# Convert Float to digit list using  list comprehension + isdigit()
res = [int(ele) for ele in str(N) if ele.isdigit()]

print("List of floating numbers is : " + str(res))

Output
List of floating numbers is : [6, 4, 5, 6]

Explanation: str(N) converts 6.456 to “6.456”, then list comprehension iterates through each character, checks if it’s a digit using isdigit() and converts it to an integer.

Using map() + regex expression + findall()

We can extract digits from a floating-point number using regular expressions. First, we convert the number into a string, then use re.findall() with the pattern \\d to find all digit characters. Finally, map(int, …) converts these extracted digits into integers and list() stores them in a list.

Python
import re

N = 6.456

# Convert Float to digit list using map() + regex expression + findall()
res = list(map(int, re.findall('\\d', str(N))))

print("List of floating numbers is : " + str(res))

Output
List of floating numbers is : [6, 4, 5, 6]

Explanation: re.findall(‘\\d’, str(N)) scans the string representation of N and extracts all digit characters, returning [‘6’, ‘4’, ‘5’, ‘6’]. map(int, …) converts them into integers, resulting in [6, 4, 5, 6]. The final list is printed as the extracted digits from the float.

Using str() + split() + map() methods

Convert the floating-point number into a list of digits by first converting it into a string and then splitting it at the decimal point using split() method. The integer and decimal parts are concatenated, and map() is used to convert each character back into an integer.

Python
N = 6.456

#Convert Float to digit list
x = str(N).split(".")
res = list(map(int, x[0]+x[1]))

print("List of floating numbers is : " + str(res))

Output
List of floating numbers is : [6, 4, 5, 6]

Explanation: str(N).split(“.”) splits 6.456 into [‘6’, ‘456’]. The integer and decimal parts are joined and passed to map(int, x[0] + x[1]), converting each digit into an integer. Finally, list() creates the final list [6, 4, 5, 6].



Next Article
Practice Tags :

Similar Reads