find the kth largest number in a list in python

One way to find the kth largest number in a list is to sort the list in descending order and then return the element at index k-1.

Here's an example implementation:

main.py
def find_kth_largest(numbers, k):
    sorted_numbers = sorted(numbers, reverse=True)
    return sorted_numbers[k-1]
116 chars
4 lines

Let's test it with a sample list:

main.py
numbers = [10, 5, 8, 3, 2, 7, 1]
k = 3
print(find_kth_largest(numbers, k)) # Output: 7
87 chars
4 lines

In this example, the 3rd largest number in the list [10, 5, 8, 3, 2, 7, 1] is 7. The function returns 7 as expected.

gistlibby LogSnag