Python Forum

Full Version: min(list) ..?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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 !
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))
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'
Thank you guys for your responses ! :)