I have a dictionary with the following structure:
{'ONE' : (4, 6, 9), 'TWO' : (3, 8, 10)}
I’d like to sort this dictionary by the 3rd value of each tuple. I can’t really think of a way to accomplish this with the data setup as a dictionary, but it should be fairly easy if I can convert the data to a nested list like the following:
[['ONE', 4, 6, 9], ['TWO', 3, 8, 10]]
I’m looking for the most efficient code for accomplishing this. If there is a way to sort this dictionary without converting it, that would be ideal. If not, any assistance with the conversion from dictionary to nested list would be greatly appreciated. Thanks.
Use the
keyargument of the built-insortedfunction:EDIT
If some of the values are ints rather than tuples, then something like this should work:
However, in the long run, it’s probably better to normalize the data so that the values all have the same format.