
On Wed, Nov 10, 2021 at 5:03 PM Christopher Barker pythonchb@gmail.com wrote:
Maybe a stupid question:
What are use cases for sorted dicts?
I don’t think I’ve ever needed one.
Me neither, tbh.
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?
Nothing's technically new. You could make an inefficient sorted dict like this:
class SortedDict(dict): def __iter__(self): return iter(sorted(self.keys()))
So in that sense, yes, it's better performance for the use-case of needing the keys to be in order. It's not something I have ever really needed - on the rare occasions when I need something like that, it's usually no harder to maintain a separate sorted list or something.
IMO this is a great tool to have on PyPI. Someone can start the naive way, run into major performance problems, and then go "huh, maybe someone else has already solved this problem". Doesn't need to be in the stdlib for that.
ChrisA