Returning a result from 3 items in a list

Chris Angelico rosuav at gmail.com
Tue Nov 24 05:14:19 EST 2015


On Tue, Nov 24, 2015 at 9:04 PM, Cai Gengyang <gengyangcai at gmail.com> wrote:
> Here's a dictionary with 3 values :
>
> results = {
>   "gengyang": 14,
>   "ensheng": 13,
>   "jordan": 12
> }
>
> How do I define a function that takes the last of the 3 items in that list and returns Jordan's results i.e. (12) ?
>
> Thanks a lot !

If you want Jordan's result, that's easy:

result["jordan"]

But there's no concept of "the last" entry in a dict. They don't have
an order. Do you mean "the entry with the greatest key"? That could be
written thus:

result[max(result)]

because max(result) is the string "jordan".

Does either of those make sense for what you're doing?

ChrisA



More information about the Python-list mailing list