sorting the list by inner elements

Sean Ross sross at connectmail.carleton.ca
Wed Mar 24 14:56:25 CET 2004


<ketulp_baroda at yahoo.com> wrote in message
news:f046efac.0403240543.65a49ba1 at posting.google.com...
> Hi, all
>
>  I want to sort the list,the form of my list is
>
>   name_city_list=[['jack','new york'],['mac','london'],['alice','paris']]
>
> all inner list has same format i.e first is name and second is city.
> now I want to sort it by the second element of the inner list(by city
name).
>
> i.e after sorting my list should be like this
>
>   name_city_list=[['mac','london'],['jack','new york'],['alice','paris']]
>
> how can I do this,
>
> any way out ?

name_city_list.sort(lambda x, y: cmp(x[1],y[1]))





More information about the Python-list mailing list