Why are there no ordered dictionaries?

Tom Anderson twic at urchin.earth.li
Fri Nov 25 21:05:36 CET 2005

On Wed, 23 Nov 2005, Christoph Zwerschke wrote:

> Tom Anderson wrote:
>>> I think it would be probably the best to hide the keys list from the 
>>> public, but to provide list methods for reordering them (sorting, slicing 
>>> etc.).
>> one with unusual constraints, so there should be a list i can manipulate in 
>> code, and which should of course be bound by those constraints.
> Think of it similar as the case of an ordinary dictionary: There is 
> conceptually a set here (the set of keys), but you cannot manipulate it 
> directly, but only through the according dictionary methods.

Which is a shame!

> For an ordedred dictionary, there is conceptually a list (or more 
> specifically a unique list). Again you should not manipulate it 
> directly, but only through methods of the ordered dictionary.
> This sounds at first more complicated, but is in reality more easy.
> For instance, if I want to put the last two keys of an ordered dict d at 
> the beginning, I would do it as d = d[:-2] + d[-2:].

As i mentioned elsewhere, i think using [] like this is a terrible idea - 
and definitely not easier.

> With the list attribute (called "sequence" in odict, you would have to 
> write: d.sequence = d.sequence[:-2] + d.sequence[-2:]. This is not only 
> longer to write down, but you also have to know that the name of the 
> attribute is "sequence".

True, but that's not exactly rocket science. I think the rules governing 
when your [] acts like a dict [] and when it acts like a list [] are 
vastly more complex than the name of one attribute.

> Python's strength is that you don't have to keep many details in mind 
> because it has a small "basic vocabulary" and orthogonal use.

No it isn't - it's in having a wide set of basic building blocks which do 
one simple thing well, and thus which are easy to use, but which can be 
composed to do more complex things. What are other examples of this kind 
of 'orthogonal use'?

> I prefer the ordered dictionary does not introduce new concepts or 
> attributes if everything can be done intuitively with the existing 
> Python methods and operators.

I strongly agree. However, i don't think your overloading of [] is at all 


Robin May

More information about the Python-list mailing list