Skip to content Skip to sidebar Skip to footer

Python: Sort Nested Dictionary By Value

I am working with a large dictionary (500+ keys) that has as its values 2 dictionaries (that have as their values numerics) and two lists (one of numerics with length=~10, one of b

Solution 1:

I'm not sure what the final form you're looking for is, since stuffing the result back into a dictionary will lose the ordering. Perhaps look into collections.OrderedDict. However, the basic idea is to just turn your dictionary into a list and then sort that list with your custom function. e.g.

answer = sorted(dd_rf.items(), key=lambda (k,v): v['rates']['correctRate'])

That's the general idea. You may want to start by seeing how something like

sorted(range(10), key=lambda x: (x-5)**2)

works to build up your understanding.

Post a Comment for "Python: Sort Nested Dictionary By Value"