I have the following structure:
structure = {
'pizza': {
# other fields
'sorting': 2,
},
'burger': {
# other fields
'sorting': 3,
},
'baguette': {
# other fields
'sorting': 1,
}
}
From this structure I need the keys of the outer dictionary sorted by the sorting field of the inner dictionary, so the output is ['baguette', 'pizza', 'burger'].
Is there a simple enough way to do this?
The
list.sort()method and thesorted()builtin function take akeyargument, which is a function that’s called for each item to be sorted, and the item is sorted based on the returnvalue of that keyfunction. So, write a function that takes a key instructureand returns the thing you want to sort on: