I have a dictionary:
d = {'a':[1,3], 'b':[3,4,5,6], 'c':[1], 'd':[1,2,3] }
I want to make a smaller, new dictionary with the top two key:value pairs, sorted by the len
of the lists in value. So in this case, I want:
newd = {'b':[3,4,5,6], 'd':[1,2,3] }
I tried this answer but got this error:
NameError: global name 'd' is not defined
One approach is to sort the items according to length of value, and then create a dictionary from the last two items.
Seems like a job for
heapq
:The advantage of
heapq
oversorted
is that this provides an O(N) time complexity whereassorted
will yield an O(NlogN) time complexity. For small dicts, this probably doesn't matter much.sorted
may even be faster due to a more optimized implementation, but for big dicts, this might actually buy you a significant speedup.(on python2.x, you can use
d.iteritems()
instead ofd.items()
)