Best way to emulate ordered dictionary (like in PHP)?

Georg Brandl g.brandl-nospam at gmx.net
Wed Apr 12 02:53:47 EDT 2006


pyGuy wrote:
> I am dealing with user profiles, which are simply a set of field-value
> pairs. I've tried to many things to list, but for one, I tried creating
> a wrapper class which inherits the dictionary and overrides the
> iterator. Unfortunately I don't understand iterators enough to get this
> working and before I waste any more time trying I figured I should
> check wether there is a better way. I have run into a similar problem
> once, and I resolved it by creating a wrapper class for the 'list'
> class which overides the get_item method, checks for string parameter,
> then accesses the appropriate item via a lookup 'list' contained within
> the class which maps the string parameter to the index number (eh, it
> just doesn't seem practical though). Now, I'e never used PHP, but
> apparently, it's dictionaries retain the order in which the items are
> entered and regurgitates them in that order when you iterate over them.
> That is exactly what I want, but can't seem to get in Python. Any help?

If you search Google for OrderedDict, you get more than enough code snippets.
There's also one in the Python Cookbook, at
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/438823

HTH,
Georg



More information about the Python-list mailing list