sorting question

belinda thom bthom at
Wed Apr 25 17:30:49 EDT 2007


I've had a look at, but am  
not sure if I can get the operator.itemgetter to do what I want for  
my particular need. I'm also not sure why creating my own cmp for  
pulling tuple parts out and passing it to a list sort doesn't just work.

I'm sure this stuff is old hat to many on this list. Suggestions  
happily accepted.

Suppose I've got a list like:

   l = [(-.3,(4,3)),(.2,(5,1)),(.10,(3,2))]

and I want to sort on the 2nd item in the 2nd tuple.

I've tried things like:

   cmp = lambda x,y : x[1][1] > y[1][1]

but l isn't then changed in place.



behaves as I'd expect, but I really want something like  
operator.itemgetter(1).itemgetter(1), which (understandably) causes a  
syntax error.



More information about the Python-list mailing list