How can I get the highest value from a dictionary nested in a list

the following is a list from a small blind auction program i am writing. After the last bid, I need to loop through all the bids in the list and print out the highest one with the name of the bidder. How can I go about that? Any help?

bids = [{'don': 200}, {'alex': 400}, {'peter': 550}]


You can use max with a custom key function:

>>> next(iter(max(bids, key=lambda d: next(iter(d.values())))))

The most annoying part of this is the next(iter(...)) part of extracting the key/value from the dictionary.

Is there any reason you use this datastructure rather than a simple dictionary like {'don': 200, 'alex': 400, 'peter': 550}? In that case it would be easier:

>>> max(bids, key=lambda name: bids[name])

Leave a Reply

Your email address will not be published. Required fields are marked *