[Python-ideas] `OrderedDict.sort`

Ram Rachum ram at rachum.com
Tue Sep 24 17:51:43 CEST 2013


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.

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


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/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130924/8c2929d9/attachment-0001.html>


More information about the Python-ideas mailing list