Returning only the highest value from a Python list

1

I have the list ['4', '07', '08', '2017', '364', '355673087875675'] and I would like a formula to return the highest value, which in this case would be '355673087875675' , I tried to use max() , but it did not work, I can not think of a simple solution for this problem, I'm thinking of using a classification algorithm but it would be too big the code, there must be some way to do that.

    
asked by anonymous 24.11.2017 / 13:01

2 answers

5

Use int as sort function for max function

>>> l = ['4', '07', '08', '2017', '364', '355673087875675']
>>> max(l, key=int)
'355673087875675'
    
24.11.2017 / 16:23
1

Tem. Use the max function itself. The problem that did not work for your code is because you have a list of strings and want to evaluate them as integer values, so you need the conversion.

numbers = ['4', '07', '08', '2017', '364', '355673087875675']

print(max(int(number) for number in numbers))  # 355673087875675

See working at Ideone | Repl.it

That is, instead of applying max direct to the list, you need to apply it to a list whose all values have been properly converted to integer.

    
24.11.2017 / 13:05