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:
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
On Sun, May 30, 2021 at 5:18 PM Stephen J. Turnbull turnbull.stephen.fw@u.tsukuba.ac.jp wrote:
Jonathan Fine writes:
tmp = list(sorted(d.items()))
The list() call is redundant. sorted() always returns a new list.
Correct.
Ditto, reversed(). The method versions are in-place.
Not correct - reversed() is a parallel to iter() and returns a reversed iterator.
ChrisA
On Mon, May 31, 2021 at 12:19 AM Stephen J. Turnbull turnbull.stephen.fw@u.tsukuba.ac.jp wrote:
Chris Angelico writes:
Ditto, reversed(). The method versions are in-place.
Not correct - reversed() is a parallel to iter() and returns a reversed iterator.
Opps, I knew that too -- once upon a time. Thanks for the correction!
To be fair, I don't think I've *ever* used reversed() in any context where it would have made a difference whether it returned a list or an iterator :)
ChrisA
On Sat, May 29, 2021 at 06:54:15PM +0200, Marco Sulla 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).
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.
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:
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). _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@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/XXA2E5... Code of Conduct: http://python.org/psf/codeofconduct/