[Python-ideas] `OrderedDict.sort`

Steven D'Aprano steve at pearwood.info
Tue Sep 24 14:13:15 CEST 2013

On Tue, Sep 24, 2013 at 04:49:20AM -0700, Ram Rachum wrote:
> What do you think about providing an `OrderedDict.sort` method? I've been 
> using my own `OrderedDict` subclass that defines `sort` for years, and I 
> always wondered why the stdlib one doesn't provide `sort`.
> I can write the patch if needed.

I'm not entirely sure why anyone would need an OrderedDict sort method. 
Ordered Dicts store keys by insertion order. Sorting the keys goes 
against the purpose of an OrderedDict.

I can understand a request for a SortedDict, that keeps the keys in 
sorted order as they are deleted or inserted. I personally don't have 
any need for one, since when I need the keys in sorted order I just 
sort them on the fly:

for key in sorted(dict):

but in any case, that's a separate issue from sorting an OrderedDict. 
Can you explain the use-case for why somebody might want to throw away 
the insertion order and replace with sorted order?


More information about the Python-ideas mailing list