I'm trying to determine the fastest runtime for getting k (key,value) pairs based on the smallest k keys in a dictionary. i.e.: for
mynahs = {40:(1,3),5:(5,6),11:(9,2),2:(6,3),300:(4,4),15:(2,8)}
smallestK(mynahs,3)
would return:
[(2,(6,3)),(5,(5,6)),(11,(9,2))]
I've seen a few different ways to do this:
1.
mylist = list(mynahs.keys())
mylist.sort
mylist = mylist[:k]
return [(k, mynahs[k]) for k in mylist]
but everyone seems to think heapq is the fastest
cheap = heapq.nsmallest(3, mynahs)
return [(k, mynahs[k]) for k in cheap]
How does heapq.nsmallest work and why is it fastest? I have seen this question and this one I still don't understand. Is heapq using a minheap to get the nsmallest? How does that work? I've also heard about an algorithm called quickselect, is that what it's using?
What's the runtime of it? If the dictionary is constantly changing/updating, is calling heapq.nsmallest each time you need the nsmallest the fastest way to do that?