Please I want to return first 6 names (only the names) with the highest corresponding integers from the list of tuple below. I have been able to return all the names from highest (sms) to lowest (boss).
[('sms', 10), ('bush', 9), ('michaels', 7), ('operations', 6), ('research', 5), ('code', 4), ('short', 3), ('ukandu', 2), ('technical', 1), ('apeh', 1), ('boss', 1)]
Thank you.
heapq.nlargest
is what you want here:
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 takes the biggest elements and discards the rest. Of course, it is less efficient than slicing if the list is pre-sorted for other reasons.
Complexity:
Explanation:
heapq.nlargest(6,your_list,key=itemgetter(1))
This line returns a list of (name,value) tuples, but only the 6 biggest ones -- comparison is done by the second (index=1 --> key=itemgetter(1)
) element in the tuple.
The rest of the line is a list-comprehension over the 6 biggest name,value tuples which only takes the name portion of the tuple and stores it in a list.
It might be of interest to you that you could store this data as a collections.Counter
as well.
d = collections.Counter(dict(your_list))
biggest = [x[0] for x in d.most_common(6)]
It's probably not worth converting just to do this calculation (that's what heapq is for after all ;-), but it might be worth converting to make the data easier to work with.
data=[('sms', 10), ('bush', 9), ('michaels', 7), ('operations', 6), ('research', 5), ('code', 4), ('short', 3), ('ukandu', 2), ('technical', 1), ('apeh', 1), ('boss', 1)]
return [x[0] for x in sorted(data, key=lambda x: x[1], reverse=True)[0:6]]
Which does following:
sorted
returns data sorted using key
function. Since standard sorting order is from ascending, reverse=True
sets it do descending;lambda x: x[1]
is anonymous function which returns second element of the argument (of a tuple in this case); itemgetter(1)
is nicer way to do this, but requires additional imports;[0:6]
slices first 6 elements of the list;[x[0] for x in ... ]
creates a list of first elements of each passed tuple;If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With