Why aren't OrderedDicts comparable with < etc?

Terry Reedy tjreedy at udel.edu
Fri Jul 17 16:59:59 EDT 2009


Sion Arrowsmith wrote:
> Jack Diederich  <jackdied at gmail.com> wrote:
>> It isn't an OrderedDict thing, it is a comparison thing.  Two regular
>> dicts also raise an error if you try to LT them.
> 
> Since when?
> 
> Python 2.5.2 (r252:60911, Jan  4 2009, 17:40:26)
> [GCC 4.3.2] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> d1 = dict((str(i), i) for i in range (10))
>>>> d2 = dict((str(i), i) for i in range (20))
>>>> d1 < d2
> True

Try reversing the definitions of d1 and d2. The dicts are probably being 
compared by id (address), which is the 2.x CPython default.

> (Don't have a 2.6 or 3 to hand.)

2.6 same. 3.1
 >>> d1,d2 = {},{}
 >>> d1 < d2
Traceback (most recent call last):
   File "<pyshell#16>", line 1, in <module>
     d1 < d2
TypeError: unorderable types: dict() < dict()




More information about the Python-list mailing list