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
3 min read
Python | Convert list of tuples into digits
Given a list of tuples, the task is to convert it into list of all digits which exists in elements of list. Letâs discuss certain ways in which this task is performed. Method #1: Using re The most concise and readable way to convert list of tuple into list of all digits which exists in elements of l
6 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
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
Python | Convert list of tuples to list of list
Converting list of tuples to list of lists in Python is a task where each tuple is transformed into list while preserving its elements. This operation is commonly used when we need to modify or work with the data in list format instead of tuples. Using numpyNumPy makes it easy to convert a list of t
3 min read
Python | Convert string enclosed list to list
Given a list enclosed within a string (or quotes), write a Python program to convert the given string to list type. Examples: Input : "[0, 2, 9, 4, 8]" Output : [0, 2, 9, 4, 8] Input : "['x', 'y', 'z']" Output : ['x', 'y', 'z'] Approach #1: Python eval() The eval() method parses the expression passe
5 min read
Python | Convert location coordinates to tuple
Sometimes, while working with locations, we need a lot of data which has location points in form of latitudes and longitudes. These can be in form of a string and we desire to get tuple versions of same. Let's discuss certain ways in which this task can be performed. Method #1 : Using tuple() + floa
5 min read
Python | Decimal to binary list conversion
The conversion of a binary list to a decimal number has been dealt in a previous article. This article aims at presenting certain shorthand to do the opposite, i.e binary to decimal conversion. Let's discuss certain ways in which this can be done. Method #1 : Using list comprehension + format() In t
6 min read
Python | Convert given list into nested list
Sometimes, we come across data that is in string format in a list and it is required to convert it into a list of the list. This kind of problem of converting a list of strings to a nested list is quite common in web development. Let's discuss certain ways in which this can be performed. Convert the
5 min read