heapq.nlargest is what you need:
import heapq from operator import itemgetter largest_names = [x[0] for x in heapq.nlargest(6,your_list,key=itemgetter(1))]
It will be more efficient than sorting, as it only accepts the largest elements and discards the rest. Of course, it is less efficient than slicing if the list is pre-sorted for other reasons.
Complexity:
- heapq: O (N)
- sort: O (NlogN)
- slicing (only if pre-sorted): O (6)
Explanation:
heapq.nlargest(6,your_list,key=itemgetter(1))
This line returns a list of tuples (name, value), but only the 6 largest ones - the comparison is performed by the second (index = 1 β key=itemgetter(1) ) element in the tuple.
The rest of the line is understanding the list by the 6 least name, tuples of values ββthat take only part of the tuple name and store it in the list.
You may be interested that you can save this data as collections.Counter .
d = collections.Counter(dict(your_list)) biggest = [x[0] for x in d.most_common(6)]
Probably you should not convert just for this calculation (which is because heapq is for everyone ;-), but it may be advisable to convert to make it easier to work with data.
mgilson
source share