how to find the longst element list of lists

Steven D'Aprano steve at
Mon Jan 8 23:30:13 CET 2007

On Mon, 08 Jan 2007 13:55:40 +0100, Peter Otten wrote:

>> The precise results depend on the version of Python you're running, the
>> amount of memory you have, other processes running, and the details of
>> what's in the list you are trying to sort. But as my test shows, sort has
>> some overhead that makes it a trivial amount slower for sufficiently small
>> lists, but for everything else you're highly unlikely to beat it.
> Try again with tN.timeit(1) and a second list that is random.shuffle()d and
> copied to L before each measurement. list.sort() treats already sorted
> lists specially.

Or, simply shuffle the list itself. Why copy it?

In my tests, sorting still wins, and by roughly the same factor.

One optimization that might shift the balance would be to remove the
list copying in the non-sort code (list_of_lists[1:]). That's going to be
very time consuming for big lists.


More information about the Python-list mailing list