[Python-Dev] Proposal: add odict to collections

Armin Ronacher armin.ronacher at active-4.com
Sun Jun 15 07:42:19 CEST 2008

Hasan Diwan <hasan.diwan <at> gmail.com> writes:

> 2008/6/14 Talin <talin <at> acm.org>:
> > There's been a lot of controversy/confusion about ordered dicts. One of the
> > sources of confusion is that people mean different things when they use the
> > term "ordered dict": In some cases, the term is used to mean a dictionary
> > that remembers the order of insertions, and in other cases it is used to
> > mean a sorted dict, i.e. an associative data structure in which the entries
> > are kept sorted. (And I'm not sure that those are the only two
> > possibilities.)
> Have the comparison function passed in as a parameter then, if it's
> None, then have it maintain the order of insertion? Something like:
> def __init__(self, cmpfunc = None):
>    self.dict = dict()
I think that would be contraproductive and would make the constructor
incompatible with the normal dict constructor which accepts keyword
arguments too.  Also that dict is just in order, but not sorted by
something.  You could still do something like this:

    d = odict()
    d['Pinguin'] = 'telly'
    d['Parrot'] = 'cage'
    d['Mouse'] = 'Napoleon'
    d.sort(key=lambda x: x[1].lower())

That of course would not sort items inserted after the sort call.


More information about the Python-Dev mailing list