Why are there no ordered dictionaries?

Ben Finney bignose+hates-spam at benfinney.id.au
Sun Nov 20 14:15:19 CET 2005


Christoph Zwerschke <cito at online.de> wrote:
> This is probably a FAQ, but I dare to ask it nevertheless since I 
> haven't found a satisfying answer yet: Why isn't there an "ordered 
> dictionary" class at least in the standard list?

What answers have you received that have not been satisfactory?

Some possible answers: The native dict is very fast, partly because
the implementation doesn't need to ensure any particular ordering.
Ordering the keys isn't the normal case, and can be done easily when
needed.

You asked "why not" rather than "has anyone done this anyway"; if
you asked the latter of the Python Cookbook, you might find something
like this:

    <URL:http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/107747>

A little old, and pre-dates subclassable native types, but quite
serviceable.

> Time and again I am missing that feature. Maybe there is something
> wrong with my programming style, but I rather think it is generally
> useful.

In what cases do you find yourself needing a dict that preserves its
key order? Can you present a use case that would be improved by an
ordered dict?

> I fully agree with the following posting where somebody complains
> why so very basic and useful things are not part of the standard
> library:

For my part, I consider it a virtue of Python that the standard
library doesn't change rapidly. It allows many competing
implementations to be shaken out before everyone starts depending on
any one of them.

> Are there plans to get it into the standard lib sometime?

Where to find an answer:

    <URL:http://www.python.org/peps/pep-0000.html>

Where to change that answer:

    <URL:http://www.python.org/peps/pep-0001.html>

-- 
 \       "One of the most important things you learn from the internet |
  `\   is that there is no 'them' out there. It's just an awful lot of |
_o__)                                         'us'."  -- Douglas Adams |
Ben Finney



More information about the Python-list mailing list