Sometimes, while working with data, we could be dealing with numbers, that are in decimal and not integers. This is general case in data science domain. Let’s discuss how to resolve a problem in which we may have a comma separated float numbers and we need to convert to float list.
Method #1 : Using list comprehension + split() + float()
The combination of above methods can be used to perform this task. In this, we convert the String to string list using split and then convert the string to float using float().
# Python3 code to demonstrate working of # Convert String float to float list # using list comprehension + split() + float() # initializing string test_str = "3.44, 7.8, 9.12, 100.2, 6.50" # printing original string print("The original string is : " + test_str) # Convert String float to float list # using list comprehension + split() + float() res = [float(idx) for idx in test_str.split(', ')] # printing result print("The float list is : " + str(res)) |
The original string is : 3.44, 7.8, 9.12, 100.2, 6.50 The float list is : [3.44, 7.8, 9.12, 100.2, 6.5]
Method #2 : Using map() + split() + float()
The combination of above functions can also be used to solve this problem. In this, we perform the task of extending logic to entire list usin map(), rest all the functionalities are performed as above method.
# Python3 code to demonstrate working of # Convert String float to float list # using map() + split() + float() # initializing string test_str = "3.44, 7.8, 9.12, 100.2, 6.50" # printing original string print("The original string is : " + test_str) # Convert String float to float list # using map() + split() + float() res = list(map(float, test_str.split(', '))) # printing result print("The float list is : " + str(res)) |
The original string is : 3.44, 7.8, 9.12, 100.2, 6.50 The float list is : [3.44, 7.8, 9.12, 100.2, 6.5]
Recommended Posts:
- Python - Convert Float String List to Float Values
- Python - Convert Float to digit list
- Convert String to Float in Python
- Python | Convert Joint Float string to Numbers
- Python | Min/Max value in float string list
- Python - Summation of float string list
- Python | List of float to string conversion
- Python - Filter float strings from String list
- Python | Convert tuple to float value
- Python program to convert float decimal to Octal number
- Python | Ways to sort list of float values
- Python | Check for float string
- float() in Python
- Python | Convert mixed data types tuple list to string list
- Python Program to convert List of Integer to List of String
- Python | Convert list of string into sorted list of integer
- Python | Convert list of numerical string to list of Integers
- Python | Convert string List to Nested Character List
- Python | sympy.Float() method
- Python | Float type and its methods
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

