<div class="gmail_quote">On Thu, Dec 3, 2009 at 8:08 PM, Tony Cappellini <span dir="ltr">&lt;<a href="mailto:cappy2112@gmail.com">cappy2112@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>I have a list of 2300 strings.<br><br>When I call max() on the list, it returned an item with 37 characters. I am only passing 1 argument to max().<br>I know for a fact that the largest item has 57 characters, and when I called mylist.index(&#39;my_57_character_string&#39;) the index was found.<br>

<br>Printing len(mylist[index]) does indeed return 57 characters.<br><br>What are the assumptions when calling max on a list of strings?<br>Does the list need to be sorted? In my case, the list is sorted.<br><br>Does max have any undocumented limitations I&#39;m not aware of?<br>
</blockquote><div><br>max() finds the &#39;largest&#39; in sort order. Strings sort in dictionary order so the max of a list strings will be the one that comes last in dictionary order.<br><br>The key= argument to sort lets you modify the way list items are compared; instead of using the default comparison of list objects, the corresponding keys are compared.<br>
<br>Try<br>  max(my_list, key=len)<br>to use the length of the strings as the key and find the longest string.<br><br>Kent<br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">

<br><dl>
<dt>
<table cellpadding="0" cellspacing="0">
<tbody>
<tr valign="baseline">
<td><b><tt>max</tt></b>(</td>
<td><var>iterable</var><big>[</big><var>, 
args...</var><big>]</big><var></var><big>[</big><var>key</var><big>]</big><var></var>)</td></tr></tbody></table>
</dt><dd>With a single argument <var>iterable</var>, return the largest item of a 
non-empty iterable (such as a string, tuple or list). With more than one 
argument, return the largest of the arguments. 
<p>The optional <var>key</var> argument specifies a one-argument ordering 
function like that used for <tt>list.sort()</tt>. The 
<var>key</var> argument, if supplied, must be in keyword form (for example, &quot;<tt>max(a,b,c,key=func)</tt>&quot;). <span>Changed in 
version 2.5: Added support for the optional <var>key</var> argument.</span> 
</p></dd></dl><br><br>
<br>_______________________________________________<br>
Tutor maillist  -  <a href="mailto:Tutor@python.org">Tutor@python.org</a><br>
To unsubscribe or change subscription options:<br>
<a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
<br></blockquote></div><br>