this is my dictionary:
mydict = {
'item_1': [7,19],
'item_2': [0,3],
'item_3': [54,191],
'item_4': [41,43]
}
and I want to sort it by values so that times with the biggest difference of the two elements in the list are placed first.
This is what I want:
mydict = {
'item_3': [54,191],
'item_1': [7,19],
'item_2': [0,3],
'item_4': [41,43],
}
How would I go about doing this?
Edit:
I got a response with this
{k: v for k, v in sorted(mydict.items(), key=lambda item: abs(item[1][0]-item[1][1]),reverse=True)}
And I also read some documentation on it, but don't understand the logic, what exactly is happening here?