[Tutor] When max() doesn't work as expected

Albert Sweigart asweigart at gmail.com
Fri Dec 4 03:20:21 CET 2009


max() for strings returns the largest string in an alphabetical sense.
So max(['z', 'aaaaaaaaaa']) would return 'z'.

You need to specify an ordering function, in your case, len():

max( ['z', 'aaaaaaaa'], key=len)

...which will return 'aaaaaaaa' because it is ordering by key.


-Al Sweigart

------------------------------------
You should check out my free book for Python beginners, "Invent Your
Own Computer Games with Python"
http://inventwithpython.com


More information about the Tutor mailing list