DEV Community

Bernice Waweru
Bernice Waweru

Posted on • Updated on

Return Two Highest Values in List

Today's challenge is rather straightforward.

Instructions

Return the two distinct highest values in a list. If there're less than 2 unique values, return as many of them, as possible.
The result should also be ordered from highest to lowest.

Examples:
[4, 10, 10, 9] => [10, 9] 
[1, 1, 1] => [1] [] => [] 
Enter fullscreen mode Exit fullscreen mode

Approach
Obtain the unique values in the list using set then sort the list in reverse order and slice to obtain the first two elements.

Solution

def two_highest(arg1):
    return sorted(set(arg1),reverse=True)[0:2]
Enter fullscreen mode Exit fullscreen mode

You are all set.

Top comments (0)