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

Lie Ryan lie.1296 at gmail.com
Fri Dec 4 03:23:35 CET 2009


On 12/4/2009 12:08 PM, Tony Cappellini wrote:
>
> What are the assumptions when calling max on a list of strings?
> Does the list need to be sorted? In my case, the list is sorted.

max determines whether one is larger than the other with "<" operator. 
max on a list of string will determine the last item if the list has 
been sorted in lexicographical order (i.e. the item closest to z, in a 
list of string with alphanumerics characters only).

If you want the longest string use:
_, longest = max((len(s), s) for s in mylist)



More information about the Tutor mailing list