[Python-ideas] `OrderedDict.sort`

M.-A. Lemburg mal at egenix.com
Tue Sep 24 20:10:36 CEST 2013


On 24.09.2013 17:51, Ram Rachum 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.

You can avoid the temp dict by doing some introspection of
the arguments and using iterators instead.

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

The use case is different. With list.sort() you don't want to create
a copy of the list, but instead have the list sort itself, since
you're not interested in the original order.

You'd only use an OrderedDict to begin with if you're interested in
the insert order, otherwise you'd start out with a plain dict().

> 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
> 

-- 
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/


More information about the Python-ideas mailing list