[BangPypers] sorting of list

Navin Kabra navin.kabra at gmail.com
Fri Jun 25 04:07:50 CEST 2010


On Fri, Jun 25, 2010 at 1:18 AM, Vikram <kpguy at rediffmail.com> wrote:

> Suppose i have this:
> >>> z1 =
> [[34,44,'1011'],[40,60,'1011'],[50,50,'1013'],[40,20,'1011'],[10,30,'1013']]
> how do i sort the nested list z1 so as to obtain:
> bla =
> [[34,44,'1011'],[40,20,'1011'],[40,60,'1011'],[10,30,'1013'],[50,50,'1013']]
>



It appears that you want to sort by the 3rd, then 1st and then 2nd element
of each sublist.

This is how you could do it:

z1.sort(key=lambda x: (x[2],x[0],x[1]))
print z1

OR sorted(z1,key=lambda x: (x[2],x[0],x[1]))


More information about the BangPypers mailing list