Why are there no ordered dictionaries?

Fuzzyman fuzzyman at gmail.com
Wed Nov 23 10:21:20 EST 2005


Rick Wotnaz wrote:
> "Fuzzyman" <fuzzyman at gmail.com> wrote in
> news:1132734447.104113.99140 at o13g2000cwo.googlegroups.com:
>
> >
> > Christoph Zwerschke wrote:
> >> - the internal keys list should be hidden
> >
> > I disagree. It is exposed so that you can manually change the
> > order (e.g. to create a "sorted" dict, rather than one ordered
> > by key insertion).
> >
> > What do you *gain* by hiding it ?
>
> The internal list should be 'hidden' in the sense that it itself
> would not be modifiable, though it should be routine to obtain a
> copy of it at any time. That copy could then be arranged as needed.
> Any rearrangement of the original list's order destroys the reason
> for having an entry- or change-ordered dict in the first place.
>

That's not the only use case. Other use cases are to have a specific
order, not based on entry time.

Simple example :

d1 = OrderedDict(some_dict.items())
d1.sequence.sort()

d1 is now an ordered dict with the keys in alphabetic order.

If you don't want to modify sequence, don't. If you want a copy do :

seq = d1.sequence[:]

All the best,

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml

> -- 
> rzed




More information about the Python-list mailing list