On Sun, May 30, 2021 at 2:57 AM Marco Sulla
<marco.sulla.pyt...@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
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/XA5JDSWRDWQKZ722EHJJHABOIAWFZV4Q/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to