More than one way to skin a cat:<br><br>>>> import operator<br>>>> sort_key = operator.itemgetter(1)<br>>>> sorted(inventory.items(),key=sort_key)[-1]<br>('oranges',525)<br><br>or...<br><br>
>>> inventory.items()<br>[('pears', 217), ('apples', 430), ('oranges', 525), ('bananas', 312)]<br>>>> count_first = [(count,fruit) for fruit,count in inventory.items()]<br>
>>> count_first<br>[(217, 'pears'), (430, 'apples'), (525, 'oranges'), (312, 'bananas')]<br>>>> max(count_first)<br>(525, 'oranges')<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> <<a href="mailto:carloslara@web.de">carloslara@web.de</a>> 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 = {'apples': 430, 'bananas': 312, 'oranges': 525, 'pears': 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>'pears'<br><br>What I would like to get is 'oranges', at least in this case.<br><br>Thanks,<br>Carlos<br><br>_______________________________________________<br>
Tutor maillist - <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>