I have a dictionary defined as such:
dict: {'KEY1': Decimal('-6.20000'), 'KEY2': Decimal('-2.58000'), 'KEY3': Decimal('6.80000')}
and I want to have either a list or an OrderedDict of the key/value pairs ordered by absolute value.
I tried:
sorted_dict = sorted(mydict, key=lambda k: abs(mydict[k]), reverse=True)
But this only returns a list of the keys, without the corresponding values, although they do seem to be sorted by the absolute values.
How can I get an OrderedDict or a list of tuples containing both the key and the values, but sorted by the absolute value?
sorted((abs(k), v) for k, v in d.items(), reverse=True)Is one waysorted((abs(v), k) for k, v in d.items(), reverse=True)