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

I think this will do it

sorted(zip(score, name), reverse=True)[:3]

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:

>>> zip(score, name)
[(350, 'Ryan'), (914, 'Stacy'), (569, 'Jenna'), (223, 'Peter'), (947, 'Sophie'), (284, 'Bryan'), (567, 'Cole'), (333, 'Andrea'), (697, 'Emily'), (245, 'Blake'), (227, 'Mike'), (785, 'Stephan'), (120, 'Rob'), (794, 'Eliza'), (343, 'Heather'), (773, 'Daniel'), (293, 'Elisabeth'), (995, 'Samantha')]

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]

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)