An ordered dictionary for the Python library?

thebjorn BjornSteinarFjeldPettersen at gmail.com
Wed Sep 12 06:28:22 EDT 2007


On Sep 12, 9:33 am, Mark Summerfield <m.n.summerfi... at googlemail.com>
wrote:
> I feel that Python lacks one useful data structure: an ordered
> dictionary.
>
> I find such data structures v. useful in C++. I know that in Python
> the sort function is v. fast, but often I prefer never to sort but
> simply to use an ordered data structure in the first place.
> (I'm aware that for ordered lists I can use the bisect module, but I
> want an ordered key-value data structure.)
[...]
> Do other Python programmers feel this lack? Is this worth a PEP?

I usually make a distinction between a sorted dict, where iteration
(and potentially positional indexing) happens in sorted key order; and
an ordered dict where items maintain insertion order. I use the latter
all the time, and e.g. Django's model metaclass does some minor magic
to overcome the fact that field-order is lost by the time your
metaclass gets control, since the attributes are passed as a regular
dict.

-- bjorn




More information about the Python-list mailing list