No cmp argument to list.sort () in Python 3.0

February 23, 2010, 10:07 pm

One of the changes introduced in Python 3.0 was the removal of the cmp argument from list.sort () and builtin.sorted (). This is all part of Python 3.0's intentionally backwards incompatible feature set, something I whole heartedly agree with (for reasons that will become clear).

I had been blissfully unaware of the performance implications of using the cmp argument (mostly because performance is rarely a consideration for the tools I write in Python) and had code like this sprinkled generously around the place:

# sort an array by the length of it's constituents l.sort(cmp=lambda x, y: len(x) - len(y))

I hadn't read the whole Python 2.4 article on sorting, which explains how cool the key argument is and how calling your key parameter once for each member in the list is much more efficient. So when I went to use the cmp argument in Python 3.0 it cheerfully failed and sent me off to the What�s New In Python 3.0 page at python.org for an explanation.

This is exactly the problem that Python 3.0 is trying to fix, unfortunately the explanation on the What's New page was sadly lacking:

builtin.sorted() and list.sort() no longer accept the cmp argument providing a comparison function. Use the key argument instead.

What I needed in addition, to this terse repetition of the error message I got from the interpreter, was something like:

The value of the key parameter should be a function that takes a single argument and returns a key to use for sorting purposes.

This all seems fairly trivial in retrospect but I spent real time hunting around for a description of this fangled key thing and built up enough angst to write this admittedly petulant post.

I doubt very much that a like minded, slightly befuddled individual will find his way here, so mostly for my own reference, sorting by string length:

def cmpLen (other): return -1 * len(other) # sort by string length (descending) l.sort (key=cmpLen)

Permalink - Tags: Development