I have a python dict that i want to order by keys and values too but i only can order it by values:
dict_to_sort = {0: 200000, 1: 858500, 2: 533800, 3: 910800, 4: 1000000}
print(dict_to_sort)
{0: 200000, 1: 858500, 2: 533800, 3: 910800, 4: 1000000}
dict_sorted = sorted(dict_to_sort.items(), key=lambda kv: kv[1])
dict_sorted = collections.OrderedDict(dict_sorted)
print(dict_sorted)
OrderedDict([(0, 200000), (2, 533800), (1, 858500), (3, 910800), (4, 1000000)])
So as you can see, the dict_sorted
has been ordered by values, but i would like to order the keys too.
The dict ordered must be looks like this:
OrderedDict([(0, 200000), (1, 533800), (2, 858500), (3, 910800), (4, 1000000)])
Can you help me?
Thank you!