Storing Python dictionary entries in the order they are pushed [duplicate] Storing Python dictionary entries in the order they are pushed [duplicate] python python

Storing Python dictionary entries in the order they are pushed [duplicate]


Try python 2.7 and above, probably 3.1, there is OrderedDict

http://www.python.org/

http://python.org/download/releases/2.7/

>>> from collections import OrderedDict>>> d = OrderedDict([('first', 1), ('second', 2),...                  ('third', 3)])>>> d.items()[('first', 1), ('second', 2), ('third', 3)]

PEP 372: Adding an ordered dictionary to collections


Implementations of order-preserving dictionaries certainly do exist.

There is this one in Django, confusingly called SortedDict, that will work in Python >= 2.3 iirc.