On 16 July, 08:12, Jack Diederich <jackd...@gmail.com> wrote: > On Thu, Jul 16, 2009 at 2:21 AM, Mark Summerfield<l...@qtrac.plus.com> wrote: > > Hi, > > > I'm just wondering why <, <=, >=, and > are not supported by > > collections.OrderedDict: > > > >>> d1 = collections.OrderedDict((("a",1),("z",2),("k",3))) > > >>> d2 = d1.copy() > > >>> d2["z"] = 4 > > >>> d1 == d2 > > False > > >>> d1 < d2 > > Traceback (most recent call last): > > File "<pyshell#6>", line 1, in <module> > > d1 < d2 > > TypeError: unorderable types: OrderedDict() < OrderedDict() > > > It just seems to me that since the items in ordered dictionaries are > > ordered, it would make sense to do an item by item comparison from > > first to last item in exactly the same way that Python compares lists > > or tuples? > >>> import this > > In the face of ambiguity, refuse the temptation to guess. > > It isn't an OrderedDict thing, it is a comparison thing. Two regular > dicts also raise an error if you try to LT them. What does it mean > for a dict to be greater than or less than its peer? Nothing, so we > refuse to guess. > > -Jack
You are right that it doesn't make sense to compare two dicts. But OrderedDicts can be viewed logically as lists of (key,value) tuples so they are much more like lists or tuples when it comes to comparisons. For example: >>> l = [("a", 1), ("z", 2), ("k", 3)] >>> l1 = l[:] >>> l1[1] = ("z", 5) >>> l < l1 True But if you do: >>> d = collections.OrderedDict(l) >>> d1 = collections.OrderedDict(l1) You can't use <, <=, =>, or >, even though ordered dictionaries preserve the order and their items are just as comparable as those in a list or tuple of tuples. -- http://mail.python.org/mailman/listinfo/python-list