[Tutor] Getting the highest numbers from a list

Remco Gerlich scarblac@pino.selwerd.nl
Tue, 28 Aug 2001 09:03:24 +0200

On  0, "David L. Lerner" <dell2100@prodigy.net> wrote:
> Is there a simple way to get the _n_ highest numbers from an unordered list?
> I have a long list that I randomly generated.  I need the five highest
> numbers.  I have another long list, also randomly generated, from which I
> need the _sum_ of the five highest numbers
> I know max(start_list) will give me the single highest number in the list
> "start_list".  How do I get the five highest numbers?

I tried to think up a few clever ways, but they turned out to be more
complicated than I thought at first. The simple way is to sort the list and
take the last five numbers. Go with the simplest way until you're certain
that is too slow (you'd need *huge* lists before some other way becomes

start_list = ...
return start_list[-5:]

Remco Gerlich