Brian Wisti wrote:
> Hi Carlos,
>
> On 6/13/07, Carlos <[EMAIL PROTECTED]> wrote:
>> Hello,
>>
>> If I have a dictionary like:
>>
>> inventory = {'apples': 430, 'bananas': 312, 'oranges': 525, 'pears': 217}
>>
>> How can I get the item with the largest quantity? I tried:
>>
>> max(inventory)
>>
>>
Hi Carlos,
On 6/13/07, Carlos <[EMAIL PROTECTED]> wrote:
> Hello,
>
> If I have a dictionary like:
>
> inventory = {'apples': 430, 'bananas': 312, 'oranges': 525, 'pears': 217}
>
> How can I get the item with the largest quantity? I tried:
>
> max(inventory)
>
> but got:
>
> 'pears'
>
> What I wou
More than one way to skin a cat:
import operator
sort_key = operator.itemgetter(1)
sorted(inventory.items(),key=sort_key)[-1]
('oranges',525)
or...
inventory.items()
[('pears', 217), ('apples', 430), ('oranges', 525), ('bananas', 312)]
count_first = [(count,fruit) for fruit,count in invent
Carlos wrote:
> Hello,
>
> If I have a dictionary like:
>
> inventory = {'apples': 430, 'bananas': 312, 'oranges': 525, 'pears': 217}
>
> How can I get the item with the largest quantity? I tried:
>
> max(inventory)
>
> but got:
>
> 'pears'
>
> What I would like to get is 'oranges', at least