Open In App
Related Articles

Python – Convert Float String List to Float Values

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Sometimes, while working with Python Data, we can have a problem in which we need to perform conversion of Float Strings to float values. This kind of problem is quite common in all domains and find application quite often. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [‘8.6’, ‘4.6’] 
Output : [8.6, 4.6] 

Input : test_list = [‘4.5’] 
Output : [4.5]

Method #1: Using list comprehension + float() The combination of above functions can be used to solve this problem. In this, we perform task of conversion using float() and list comprehension is used to perform iteration. 

Step-by-step approach :

  • A new list ‘res’ is created using list comprehension that converts each element of ‘test_list’ to float using the ‘float()’ function. The ‘for’ loop in the list comprehension loops through each element in ‘test_list’ and converts it to float.
  • The converted list ‘res’ is printed using the ‘print()’ function.

Below is the implementation of the above approach:

Python3

# Python3 code to demonstrate working of
# Convert Float String List to Float Values
# Using float() + list comprehension
 
# initializing list
test_list = ['87.6', '454.6', '9.34', '23', '12.3']
 
# printing original list
print("The original list : " + str(test_list))
 
# Convert Float String List to Float Values
# Using float() + list comprehension
res = [float(ele) for ele in test_list]
 
# printing result
print("List after conversion : " + str(res))

                    
Output : 
The original list : ['87.6', '454.6', '9.34', '23', '12.3']
List after conversion : [87.6, 454.6, 9.34, 23.0, 12.3]

Time complexity: O(n), where n is the length of the input list. This is because the program uses a list comprehension to iterate over each element of the input list and convert it to a float value, which takes constant time for each element. Therefore, the total time taken by the program is proportional to the length of the input list.
Auxiliary space: O(n), because the program creates a new list to store the float values obtained by converting the input list. The space required by the output list is proportional to the length of the input list, so the auxiliary space complexity is also proportional to n.

Method #2: Using map() + float() The combination of above functions can also be used to solve this problem. In this, we perform the task of conversion using float and extension of conversion logic using map(). 

Python3

# Python3 code to demonstrate working of
# Convert Float String List to Float Values
# Using map() + float()
 
# initializing list
test_list = ['87.6', '454.6', '9.34', '23', '12.3']
 
# printing original list
print("The original list : " + str(test_list))
 
# Convert Float String List to Float Values
# Using map() + float()
res = list(map(float, test_list))
 
# printing result
print("List after conversion : " + str(res))

                    
Output : 
The original list : ['87.6', '454.6', '9.34', '23', '12.3']
List after conversion : [87.6, 454.6, 9.34, 23.0, 12.3]

Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list. 

Method 3: Using a for loop and the float() function.

This code achieves the same result as the original code, but it uses a for loop and the float() function instead of the map() function.

Python3

# initializing list
test_list = ['87.6', '454.6', '9.34', '23', '12.3']
 
# printing original list
print("The original list : " + str(test_list))
 
# Convert Float String List to Float Values using for loop and float() function
res = []
for val in test_list:
    res.append(float(val))
 
# printing result
print("List after conversion : " + str(res))

                    

Output
The original list : ['87.6', '454.6', '9.34', '23', '12.3']
List after conversion : [87.6, 454.6, 9.34, 23.0, 12.3]

Time complexity: O(n), where n is the length of the input list. 
Auxiliary space: O(n), where n is the length of the input list. 

Method 4: Using the NumPy library

 step-by-step approach :

  1. Import the NumPy library using the import numpy as np statement.
  2. Initialize a list of float strings named test_list.
  3. Print the original list using the print() function and the string “The original list : ” concatenated with the string representation of test_list.
  4. Use the np.array() function from the NumPy library to convert test_list to a NumPy array of float values.
  5. This is done by passing test_list as the first argument and dtype=float as the second argument, which specifies that the data type of the resulting array should be float. The resulting array is stored in a variable named res.
  6. Print the resulting NumPy array using the print() function and the string “List after conversion : ” concatenated with the string representation of res.
  7. That’s it! The program prints the original list and the list after conversion to float values using the NumPy library.

Python3

import numpy as np
 
# initializing list
test_list = ['87.6', '454.6', '9.34', '23', '12.3']
 
# printing original list
print("The original list : " + str(test_list))
 
# Convert Float String List to Float Values using NumPy
res = np.array(test_list, dtype=float)
 
# printing result
print("List after conversion : " + str(res))

                    
OUTPUT:
The original list : ['87.6', '454.6', '9.34', '23', '12.3']
List after conversion : [ 87.6  454.6    9.34  23.    12.3 ]

Time complexity: O(n)

Auxiliary space: O(n), where n is the length of the input list.



Last Updated : 05 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads