-1

I have a dict as in below:

saving = {0: (1, 2, 9.0), 1: (1, 3, 603.0), 2: (1, 4, 396.0), 3: (1, 5, 9.0), 4: (2, 3,     206.0)} 

I want to sort descending this dict order by the third value.
I tried sorted and sort() but I couldn't sort the dict. I don't know much about the usage sort and sorted functions. Can you help me ? Thanks.

Tim
  • 41,901
  • 18
  • 127
  • 145

1 Answers1

0

Probably OrderedDict is what you're looking for. It behaves like a dictionary, but its keys can be sorted. Standard python dictionaries are inherently unordered.

In your case this should work:

d = OrderedDict(sorted(saving.items(), key=lambda t: t[1][2]))
minder
  • 2,059
  • 5
  • 24
  • 39