[EMAIL PROTECTED] wrote: > > Lots and lots of people want ordered dicts it seems. Or at least, they > > want > > to be able to access their dictionary keys in order. > > [snipped lots of examples, nice pro-con lists, etc.] > > What do y'all think? > > I'll second the need for this. Although, what can also be useful as a > further extension (at least I needed this capability for a project I > worked on) is the ability to not only have the keys sorted, but have them > arranged in an arbitrary order (I had parsed some XML data, and each > "object" represented in the XML had certain fields, which I was storing in > a dictionary, but I needed each object to iterate that dictionary in the > same order, preferably the order they occured in the file). > > I accomplished this through subclassing dict to create a > SpecialSortedDict, overriding keys(), values(), the iterxxx() functions, > and any others relevant so that it had an associated key order list. It > would return keys in the specified order (ignoring any in the order list > that weren't in the dictionary), an then any remaing keys were sorted at > the end. > > I don't know if that'd be a useful enhancement to Chris's idea, or if it's > a really obscure corner case. But I thought I'd throw it out there.
The need to have an ordered dictionaries is fairly common, IMHO. Having one with keys sorted alphabetically is just a specific type of ordering. Russell Owen of the University of Washington has long had a class that supports exactly these capabilities in his free RO Python package (along with a number of other useful things), see <http://www.astro.washington.edu/owen/ROPython.html>. Owen's 'OrderedDict' class is [also] implemented by subclassing dict. To avoid continued reinvention of this wheel, I'd also vote to have this functionality be at least included in a standard module, if not built-in. They say necessity is the mother of invention, therefore a lack of standards or built-ins must be the mother of reinvention. ;-) -Martin -- http://mail.python.org/mailman/listinfo/python-list