On Mon, Nov 17, 2008 at 1:56 AM, asc <[EMAIL PROTECTED]> wrote: > 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?
You use the `key` argument to .sort(): L2.sort(key=lambda item: item[1]) This sorts L2 by the result of applying the `key` function to each of the items. Behind the scenes, I believe it does a Schwartzian transform. Cheers, Chris -- Follow the path of the Iguana... http://rebertia.com > > Many thanks. > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list