An ordered dictionary for the Python library?

James Stroud jstroud at mbi.ucla.edu
Fri Sep 14 15:25:22 EDT 2007


Mark Summerfield wrote:
> So to clarify, here's the entire API I'm proposing for ordereddict. In
> all cases the ordereddict is always in (and kept in) key order, and
> any method that returns a list or iterator always returns that list or
> iterator (whether of keys or values) in key order:
> 
> ordereddict(dictionary=None)
>     The argument can be a dict or another ordereddict; all the dict()
> initializer
>     approaches should also be supported.
> 
> ordereddict.update(dictonary=None, **kwargs)
>     Same as dict.update()---except that key order is preserved (a
> point I won't
>     repeat in the others when I say "same as dict", but which is true
> throughout)
> 
> @classmethod
> ordereddict.fromkeys(cls, iterable, value=None) # Same as dict
> 
> ordereddict.key(index : int) -> key
>     Returns the index-th item's key
> 
> ordereddict.item(index : int) -> (key, value)
>     Returns the index-th item
> 
> ordereddict.value(index : int) -> value
>     Returns the index-th item's value
> 
> ordereddict.set_value(index : int, value)
>     Sets the index-th item's value to value; raises IndexError if
> index is out of
>     range. If not expensive, maybe return the key.
> 
> ordereddict.copy() # Same as dict.
> ordereddict.clear() # Same as dict.
> ordereddict.get(key, value=None) # Same as dict
> ordereddict.setdefault(key, value) # Same as dict
> ordereddict.pop(key, value) # Same as dict
> ordereddict.popitem() # Same as dict
> 
> ordereddict.keys(fromindex : int = None, uptoindex : int : None) ->
> list of keys
>     Returns an ordered list of keys, or a slice of keys if one or two
> indexes is given
> 
> ordereddict.values() # Same as dict
> ordereddict.items() # Same as dict
> ordereddict.iterkeys() # Same as dict
> ordereddict.itervalues() # Same as dict
> ordereddict.iteritems() # Same as dict
> ordereddict.has_key() # Same as dict
> 
> Also the same as dict (and as always, working in key order):
> 
> for key in d: pass
> if key in d: pass
> len(d)
> del d[key]
> d[key]
> d[key] = value

May I also make one more suggestion, to call it a "sort_ordered_dict" 
(or "sortordereddict", or even better a "sorteddict"--where the "ed" 
comes from "ordered")? Its hard for me to move past the established 
definition of "order", as we think of tuples being ordered--as in the 
first sentence of http://en.wikipedia.org/wiki/Tuple--to something that 
is preserving an order according to a comparison. The distinction is so 
firmly ingrained in my head that it took me a while to wake up to the 
fact that you were describing something completely different than an 
ordered dictionary (e.g. http://www.voidspace.org.uk/python/odict.html) 
even though you were being very unambiguous with your description.

And I also think the ability to drop it in for a built-in dict is very 
valuable.

James



More information about the Python-list mailing list