I`ve been trying to convert a string numbers list into float and get the max of this list but I keep getting this error:
ValueError: could not convert string to float: '.'
My attempt was to get a list like ls = ['1','2','3','0.5'] and convert using this function
def convert_to_int(ls: list):
values = [float(a) for a in ls]
return (len(values), values)
print(max(ls, key=convert_to_int))
Why I’m having this error? Can someone help me?
>Solution :
I think, what you’re looking for is:
max(ls, key=float)
output: '3'
This will get the max value of your string list, using a temporary float conversion within max