Python Forum
min(list) ..? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: min(list) ..? (/thread-11951.html)



min(list) ..? - zuzulic - Aug-02-2018

Hi guys,

I have a little question. Hopefully that somebody can help me :) :
If I have this list:
L=['13', '2', '3', '3', '5', '7']
min(L) return '13'.

Why 13 and not 2 ?

Thank you !


RE: min(list) ..? - Larz60+ - Aug-02-2018

This is because you have a list of strings, not integers
try:
list_of_ints = [13, 2, 3, 5, 7]
print(min(list_of_ints))



RE: min(list) ..? - volcano63 - Aug-02-2018

Strings are compared lexicographically - character by character - till the bigger/smaller is defined. That means that "2" is larger than "13", because the first character of the string "2" is bigger than the first character of the string "13"

You could use the named argument key of the min function to get the result
min(L, key=int)
The result in the string form, but values are compared by their integer equivalent

Output:
In [64]: L=['13', '2', '3', '3', '5', '7'] In [65]: min(L, key=int) Out[65]: '2'



RE: min(list) ..? - zuzulic - Aug-02-2018

Thank you guys for your responses ! :)