
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).

On Sun, May 30, 2021 at 2:57 AM Marco Sulla <Marco.Sulla.Python@gmail.com> wrote:
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

On Sat, May 29, 2021 at 06:54:15PM +0200, Marco Sulla wrote:
Dicts keep their insertion order stable, that doesn't mean that they are sortable. They aren't lists where you can just reorder items, the implementation is more complex than a simple list of (key,value) pairs that can be sorted. My understanding of the implementation is that the only practical way to sort a dict is to sort the items ahead of time and then build it. I don't know the current implementation, but I think it *might* be something like Raymond Hettinger's proposal for a compact, order-preserving, key-sharing dict: https://mail.python.org/pipermail/python-dev/2012-December/123028.html If you can think of a way to sort something like that, without changing the way hash lookups work, and more efficiently than just sorting the items and building a new dict, remember that the implementation can change at any time. The bottom line here is that *dicts aren't lists* and it doesn't make sense to sort them. If you want a sortable data structure, stick to a list or a tree, or a specialised data structure designed for the task. -- Steve

FWI, this is a previous thread. https://discuss.python.org/t/add-a-dict-sort-method/5747 2021年5月30日(日) 1:57 Marco Sulla <Marco.Sulla.Python@gmail.com>:

On Sun, May 30, 2021 at 2:57 AM Marco Sulla <Marco.Sulla.Python@gmail.com> wrote:
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

On Sat, May 29, 2021 at 06:54:15PM +0200, Marco Sulla wrote:
Dicts keep their insertion order stable, that doesn't mean that they are sortable. They aren't lists where you can just reorder items, the implementation is more complex than a simple list of (key,value) pairs that can be sorted. My understanding of the implementation is that the only practical way to sort a dict is to sort the items ahead of time and then build it. I don't know the current implementation, but I think it *might* be something like Raymond Hettinger's proposal for a compact, order-preserving, key-sharing dict: https://mail.python.org/pipermail/python-dev/2012-December/123028.html If you can think of a way to sort something like that, without changing the way hash lookups work, and more efficiently than just sorting the items and building a new dict, remember that the implementation can change at any time. The bottom line here is that *dicts aren't lists* and it doesn't make sense to sort them. If you want a sortable data structure, stick to a list or a tree, or a specialised data structure designed for the task. -- Steve

FWI, this is a previous thread. https://discuss.python.org/t/add-a-dict-sort-method/5747 2021年5月30日(日) 1:57 Marco Sulla <Marco.Sulla.Python@gmail.com>:
participants (6)
-
Chris Angelico
-
Inada Naoki
-
Jonathan Fine
-
Marco Sulla
-
Stephen J. Turnbull
-
Steven D'Aprano