[Python-Dev] PEP 265 - Sorting dicts by value

David Harrison dave.l.harrison at gmail.com
Mon Sep 13 03:34:06 CEST 2004


Hi all,

Quick pep265 summary : People frequently want to count the occurrences
of values in a dict, or sort the results of a d.items() call by value.
 This could be done by extending the current items() definition, or by
creating a new function for the dict object (both requiring a C
implementation).

I've had a read through pep265 a few times now, and every time I've had
two immediate reactions.

First, that I've been there too.  I've found myself innumerable times
needing to count the occurrences of values in a dict.  However second,
that dicts shouldn't be naturally sortable.  A dict does not guarantee the
order that it returns calls such as items() keys() or values().  It's
my feeling that
we should not encourage people to rely on a dict returning a set ordering, since
as a hash based data structure they are designed for key lookup not
sequential traversal - if you want to sort something, massage the data
into a list and then sort the list (I've seen a proposal before that
the sort function be able to handle objects which would allow sorting
of 2 dimensional lists).

With regards to the two arguments put forward by Grant, the first - that
it is an idiom known only to experienced campaigners - does not seem to
be a supportable argument to me.   I think the problem has quite a
simple elegant solution which is rather easily discovered - there are
lots of differences in Python that require an inexperienced programmer
to learn a new idiom (such as the looping construct).

The second, that the solution is full of 'grunge', seems a matter of
taste and use to me.  As mentioned in the pep there are different kinds
of comparison that may be wanted, but could not be supported.  Further,
it is a natural use case of a dict that items held within it need not be
of the same type (and therefore makes the idea of a comparison between
them meaningless).

With respect to implementation suggestions, numbers 1 2 and 3 definitely
don't work for me.  To extend the usage of items() without similarly
extending the usage of keys() and values() would mean that we are
special casing the items() function in a way that makes it inconsistant
with the other dict functions.  Number 5 seems too specific to me.  I
could live with 4 ;-)

I think in the end it's my feeling that these kind of idioms belong in
the cookbook - which, incidentally, it already is to a certain extent
under 'Sorting a Dictionary', another recipe could always be added
for this ;-)

cheers
Dave Harrison


More information about the Python-Dev mailing list