Skip to content
Related Articles

Related Articles

Python – Convert Float String List to Float Values
  • Last Updated : 03 Jul, 2020

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


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]

 

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().

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


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]

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :