I have a simple dictionary (old_d) in the form of {'stringkey': intvalue, ...} and I want to order it by value. To do that I use.
new_d = {k: v for k, v in sorted(old_d.items(), key=lambda x: x[1])}
If I use print(new_d) this works, but when I use pprint(new_d) it returns me the dictionary ordered by key name and not by value. I would like to use pprint because these dictionaries have a lot of objects and the output cant't be on one line, how can I do that?
pprint()
has a keyword argumentsort_dicts
which solves your problem, but it is only available since python 3.8 version.