On Tue, Nov 09, 2021 at 10:01:35PM -0800, Christopher Barker wrote: > Maybe a stupid question: > > What are use cases for sorted dicts? > > I don’t think I’ve ever needed one.
Good question :-) > Also, I can’t quite tell from the discussion If a “sorted dict” implements > something new, or is an internal data structure that gives better > performance for particular use cases. I.e. is a sorted dict a Mapping? All dicts are mappings :-) I would expect any kind of sorted dict to support the full mutable mapping interface. Some mappings are not necessarily implemented as hash tables, as dict is. Some variety of tree is a common choice. I expect that a sorted dict (however it is implemented) would be a mapping that preserves sorted order on insertions and deletions, rather than insertion order. (Or some arbitrary order.) I haven't used one myself, but I would expect an API where you create a Sorted Dict (of whatever library or implementation you prefer), set an optional key function and direction (ascending or descending), then as you insert keys, the mapping keeps the keys in sort order. Note that this is different from *sorting* a dict, which (if it were supported) has to be done explicitly. Between sorts, the dict would be capable of getting out of sorted order. The idea of a *sorted* dict is that the sort order is an invariant, rather than something that can come and go as you insert and delete items. -- Steve _______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-le...@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/JCFQYOSD5NQ3GFGLRVW7QWISXQL5LHS5/ Code of Conduct: http://python.org/psf/codeofconduct/