If you want to extract n max values from a large Python list, using heapq
will speed up the code.
In the code above, using heapq
is 2.503 times faster than using sorting and indexing. Both methods try to find the max values of a list of 10000 items.