Why are there no ordered dictionaries?

Fredrik Lundh fredrik at pythonware.com
Mon Nov 21 06:55:18 EST 2005


Ben Sizer wrote:

> > No, that's not the same logic.  The dict() in my example doesn't convert be-
> > tween data types; it provides a new way to view an existing data structure.
>
> This is interesting; I would have thought that the tuple is read and a
> dictionary created by inserting each pair sequentially. Is this not the
> case?

pointers to the members of each pair, yes.  but a pointer copy is a
cheap operation (for the given use case, we're only talking about a
few dozen pairs anyway, at the most).

this is a common fallacy; Python programmers underestimate the
cost of adding extra layers to their code (e.g. by using an ordered
dict data structure that has to incrementally update both a list and
a dictionary), and overestimate the cost of letting the C layer do
bulk operations.

(as an example, on my machine, using Foord's OrderedDict class
on Zwerschke's example, creating the dictionary in the first place
takes 5 times longer than the index approach, and accessing an
item takes 3 times longer.  you can in fact recreate the index 6
times before OrderedDict is faster; if you keep the index around,
the OrderedDict approach never wins...)

</F>






More information about the Python-list mailing list