I want to sort a dictionary of lists, by third item in each list. It’s easy enough sorting a dictionary by value when the value is just a single number or string, but this list thing has me baffled.
Example:
myDict = {'item1': [7, 1, 9], 'item2': [8, 2, 3], 'item3': [9, 3, 11] }
I want to be able to iterate through the dictionary in order of the third value in each list, in this case item2, item1 then item3.
Here is one way to do this:
The
keyargument of thesortedfunction lets you derive a sorting key for each element of the list.To iterate over the keys/values in this list, you can use something like: