# Sorting a list of lists

R (Chandra) Chandrasekhar chyavana at gmail.com
Fri Feb 12 18:45:35 CET 2010

```Dear Folks,

I have lines of values like so:

14, [25, 105, 104]
10, [107, 106, 162]
21, [26, 116, 165]

I need to sort them in two ways:

(a) By the numeric value of the first column; and

(b) by the sum of the elements of the second item in each list, which is
a list in itself.

At present, I have appended each line into a list L so that I for teh
above minimal data, I have a list of lists thus:

[[14, [25, 105, 104]], [10, [107, 106, 162]], [21, [26, 116, 165]]]

I have tried using

(a) sorted(L, key = lambda x:(x[0]))

and

(b) sorted(L, key = lambda x:(sum(x[1])))

and get the anticipated results for (a) and (b0, at least with the above
minimal data set.

Is this a sensible way to go about the sorting, or are there better ways
of doing it in Python?

Also, I am baffled because the above fails obviously when len(L) is
about a hundred and I can't figure out why.

TIA

Chandra

```