Sorting lists

asc adam.cheasley at gmail.com
Mon Nov 17 04:56:08 EST 2008


Hi all,
I have a problem and I'm not sure whether sort() can help me.
I understand that if I have a list; say L = ['b', 'c', 'a']
I can use L.sort() and I will then have; L = ['a', 'b', 'c']

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? Or am I going to have write a
custom function to sort this out?

Many thanks.



More information about the Python-list mailing list