On 17/11/06, Dave S <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I have a bunch of lists within lists that I need to sort ref item [4], I can't
> access my code at the moment but I basically ...
>
[...]
>
> Having googled I think there is a better way of doing this with the key
> attribute
>
> a.sort(key= ?? )

Yep, that's right.  You need at least python2.4 for this, though.

You need to pass the key= parameter a function that will take an
element of your list, and return the comparison key.

eg:

def item4(elem):
    return elem[4]

a.sort(key=item4)

Since this is a common thing to do, there is a function in the
operator module that you can use, instead of defining your own:

import operator
a.sort(key=operator.itemgetter(4))

(operator.itemgetter(4) will return a function that is basically
equivalent to item4() above)

HTH!

-- 
John.
_______________________________________________
Tutor maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/tutor

Reply via email to