Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 6 years ago.
Improve this question
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.
sorted_items = sorted(d.items(), key = lambda item : len(item[1]))
newd = dict(sorted_items[-2:])
Seems like a job for heapq
:
big_items = heapq.nlargest(2, d.items(), key=lambda x: len(x[1]))
newd = dict(big_items)
The advantage of heapq
over sorted
is that this provides an O(N) time complexity whereas sorted
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 of d.items()
)