Skip to content
Advertisement

How do I index the 3 highest values in a list?

so i have these 2 lists:

JavaScript

I have to find the 3 highest scores and tally these scores with their respective scorers in the name list, so that I can have new lists

JavaScript

I’m thinking of indexing the highest scores, appending them into a list than using the index to tally these scores with the names.

my question is how do i index the 3 highest values in the list? and then, how do i use the index to look for the scorers name in the name list so that i can append them in the top3name list?

Advertisement

Answer

I think this will do it

JavaScript

So you understand what is going on:

zip: takes iterables as it’s arguments and takes one element from each iterable, placing them in a tuple.

So:

JavaScript

sorted: will sort the data. By default, a tuple element is sorted on the element in the 0 index, so the score in this case. Reverse=True will sort it descending first.

And lastly, the [:3] is slice notation, saying give me all elements from the beginning up to the 3rd element. This could have also been written as [0:3]

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement