Python | Min/Max value in float string list

Sometimes, while working with a Python list, we can have a problem in which we need to find min/max value in the list. But sometimes, we don’t have a natural number but a floating-point number in string format. This problem can occur while working with data, both in web development and Data Science domain. Let’s discuss a way in which this problem can be solved.

Method : Using min()/max() + float()
This problem can be solved using the min or max function in which we first convert the strings into float and then pass this logic in functions in respective min/max function.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Min / Max value in float string list
# using min()/max() + float() + generator
  
# initialize lists
test_list = ['4.5', '7.8', '9.8', '10.3']
  
# printing original list
print("The original list is : " + str(test_list))
  
# Min / Max value in float string list
# using min()/max() + float() + generator
res_min = min(float(sub) for sub in test_list)
res_max = max(float(sub) for sub in test_list)
  
# printing result
print("The min value of list : " + str(res_min))
print("The max value of list : " + str(res_max))

chevron_right


Output :

The original list is : ['4.5', '7.8', '9.8', '10.3']
The min value of list : 4.5
The max value of list : 10.3


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.