Python - Convert Float to digit list
Last Updated :
24 Feb, 2025
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))
OutputList 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))
OutputList 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))
OutputList 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))
OutputList 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))
OutputList 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].
Similar Reads
Convert String Float to Float List in Python We are given a string float we need to convert that to float of list. For example, s = '1.23 4.56 7.89' we are given a list a we need to convert this to float list so that resultant output should be [1.23, 4.56, 7.89].Using split() and map()By using split() on a string containing float numbers, we c
2 min read
Python - List of float to string conversion When working with lists of floats in Python, we may often need to convert the elements of the list from float to string format. For example, if we have a list of floating-point numbers like [1.23, 4.56, 7.89], converting them to strings allows us to perform string-specific operations or output them
3 min read
Convert Float String List to Float Values-Python The task of converting a list of float strings to float values in Python involves changing the elements of the list, which are originally represented as strings, into their corresponding float data type. For example, given a list a = ['87.6', '454.6', '9.34', '23', '12.3'], the goal is to convert ea
3 min read
Convert Floating to Binary - Python The task of converting a floating-point number to its binary representation in Python involves representing the number in the IEEE 754 format, which consists of a sign bit, an exponent and a mantissa. For example, given the floating-point number 10.75, its IEEE 754 32-bit binary representation is "0
3 min read
How to Convert Binary Data to Float in Python? We are given binary data and we need to convert these binary data into float using Python and print the result. In this article, we will see how to convert binary data to float in Python. Example: Input: b'\x40\x49\x0f\xdb' <class 'bytes'>Output: 3.1415927410125732 <class 'float'>Explana
2 min read