[Python-ideas] `OrderedDict.sort`

Andrew Barnert abarnert at yahoo.com
Tue Sep 24 18:27:08 CEST 2013


On Sep 24, 2013, at 8:51, Ram Rachum <ram at rachum.com> wrote:

> I get your point. It's a nice idea. But I think it's slightly less elegant to create another dict. So I think it's almost as good as having a `.sort` method, but not quite as nice.

Honestly, I think having a sorted mapping in the stdlib would be even nicer in almost any situation where this might be nice. But, given that we don't have such a thing, and getting one into the stdlib is harder than it appears, maybe that's not an argument against your (obviously simpler) idea.

Of course in most cases, you just want to iterate once in sorted order, and it's hard to beat this:

    for k, v in sorted(o.items()):

> (By the way, couldn't you make the same argument about `list.sort`?)

You could. Except that list.sort predates sorted. And it's faster and saves memory, which isn't true of your suggestion. I don't know if that would be enough to add it today, but it's more than enough to keep it around.

> On Tue, Sep 24, 2013 at 6:49 PM, M.-A. Lemburg <mal at egenix.com> wrote:
>> On 24.09.2013 17:23, Ram Rachum wrote:
>> > Ethan, you've misunderstood my message and given a correct objection to an
>> > argument I did not make.
>> >
>> > I did not argue against ordering by insertion order on init. I agree with
>> > that decision. I disagree with defining the entire class as an insertion
>> > ordering class and refusing to allow users to reorder it as they wish after
>> > it's created.
>> 
>> The overhead introduced by completely recreating the internal
>> data structure after the sort is just as high as creating a
>> new OrderedDict, so I don't understand why you don't like about:
>> 
>> from collections import OrderedDict
>> o = OrderedDict(((3,4), (5,4), (1,2)))
>> p = OrderedDict(sorted(o.iteritems()))
>> 
>> This even allows you to keep the original insert order should
>> you need it again. If you don't need this, you can just use:
>> 
>> o = dict(((3,4), (5,4), (1,2)))
>> p = OrderedDict(sorted(o.iteritems()))
>> 
>> which is also faster than first creating an OrderedDict and
>> then recreating it with sorted entries.
>> 
>> Put those two lines into a function and you have:
>> 
>> def SortedOrderedDict(*args, **kws):
>>     o = dict(*args, **kws)
>>     return OrderedDict(sorted(o.iteritems()))
>> 
>> p = SortedOrderedDict(((3,4), (5,4), (1,2)))
>> 
>> --
>> Marc-Andre Lemburg
>> eGenix.com
>> 
>> Professional Python Services directly from the Source  (#1, Sep 24 2013)
>> >>> Python Projects, Consulting and Support ...   http://www.egenix.com/
>> >>> mxODBC.Zope/Plone.Database.Adapter ...       http://zope.egenix.com/
>> >>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
>> ________________________________________________________________________
>> 2013-09-11: Released eGenix PyRun 1.3.0 ...       http://egenix.com/go49
>> 2013-09-28: PyDDF Sprint ...                                4 days to go
>> 2013-10-14: PyCon DE 2013, Cologne, Germany ...            20 days to go
>> 
>>    eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
>>     D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
>>            Registered at Amtsgericht Duesseldorf: HRB 46611
>>                http://www.egenix.com/company/contact/
> 
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130924/49f51d33/attachment.html>


More information about the Python-ideas mailing list