Sorting lists

John Machin sjmachin at
Mon Nov 17 11:07:39 CET 2008

On Nov 17, 8:56 pm, asc <adam.cheas... at> wrote:

> But my problem is this. I have a list, that contains a number of
> embeded lists;
> e.g. L2 = [['something', 'bb'], ['somethingElse', 'cc'],
> ['anotherThing', 'aa']]
> Now I want to sort this list by the second item of each sublist. So
> the outcome I would like is;
> L2 = [['anotherThing', 'aa'], ['something', 'bb'], ['somethingElse',
> 'cc']]
> Is there a way I can use sort for this?

Yes. Read the manual. Look for the key argument. You need to make up a
function (using def or lambda) that will pluck the desired sort-key
out of a sub-list.

More information about the Python-list mailing list