
May 29, 2021
5 p.m.
On Sun, May 30, 2021 at 2:57 AM Marco Sulla <Marco.Sulla.Python@gmail.com> wrote:
Since `dict` now is ordered, how about a `sort()` method? It could have the same signature of list.sort(), with an optional parameter "by" that can be "keys" or "values" ("keys" could be the default).
Not really a thing - if you want that level of flexibility, try OrderedDict, which lets you move elements around. But if you're okay with constructing a new dict, you can do this: d = dict(sorted(d.items(), key=lambda kv: ...)) Your key function will receive a tuple of the key and the value. If you don't provide one, default tuple sorting will effectively sort the elements by their keys - probably a good default. ChrisA