+ 3

The min function ( Python )

What would happen if I used the min function when there is a list of strings and no numbers?

15th Dec 2018, 11:48 AM
Monya 🇮🇶
Monya 🇮🇶 - avatar
3 odpowiedzi
+ 2
It returns the smallest string. Strings are compared lexicographically, that means the smallest string is the first in alphabetical ordering. min(["Sololearn", "Python", "Questions"]) returns Python
15th Dec 2018, 3:42 PM
michal
+ 1
If there are only strings in it, it compares the strings. If you have mixed types convert them with str() or int() or it failes.
15th Dec 2018, 11:54 AM
Yannick Reiß
Yannick Reiß - avatar
+ 1
What about just trying it in the code playground? ;) You will then notice, that the string which comes alphabetically first is chosen. So e.g. for ["bla","gibberish"] the min function will output bla. If I'm not mistaken it is also part in the Python course. Or maybe only at sorting 🤔 I don't know anymore 😅
15th Dec 2018, 11:55 AM
Matthias
Matthias - avatar