More than one way to skin a cat:<br><br>&gt;&gt;&gt; import operator<br>&gt;&gt;&gt; sort_key = operator.itemgetter(1)<br>&gt;&gt;&gt; sorted(inventory.items(),key=sort_key)[-1]<br>(&#39;oranges&#39;,525)<br><br>or...<br><br>
&gt;&gt;&gt; inventory.items()<br>[(&#39;pears&#39;, 217), (&#39;apples&#39;, 430), (&#39;oranges&#39;, 525), (&#39;bananas&#39;, 312)]<br>&gt;&gt;&gt; count_first = [(count,fruit) for fruit,count in inventory.items()]<br>
&gt;&gt;&gt; count_first<br>[(217, &#39;pears&#39;), (430, &#39;apples&#39;), (525, &#39;oranges&#39;), (312, &#39;bananas&#39;)]<br>&gt;&gt;&gt; max(count_first)<br>(525, &#39;oranges&#39;)<br><br>And then of course you could iterate over the dictionary setting up variables that hold the highest fruit and count found so far.
<br><br><br><br><div><span class="gmail_quote">On 6/13/07, <b class="gmail_sendername">Carlos</b> &lt;<a href="mailto:carloslara@web.de">carloslara@web.de</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hello,<br><br>If I have a dictionary like:<br><br>inventory = {&#39;apples&#39;: 430, &#39;bananas&#39;: 312, &#39;oranges&#39;: 525, &#39;pears&#39;: 217}<br><br>How can I get the item with the largest quantity? I tried:
<br><br>max(inventory)<br><br>but got:<br><br>&#39;pears&#39;<br><br>What I would like to get is &#39;oranges&#39;, at least in this case.<br><br>Thanks,<br>Carlos<br><br>_______________________________________________<br>
Tutor maillist&nbsp;&nbsp;-&nbsp;&nbsp;<a href="mailto:Tutor@python.org">Tutor@python.org</a><br><a href="http://mail.python.org/mailman/listinfo/tutor">http://mail.python.org/mailman/listinfo/tutor</a><br></blockquote></div><br>