Hi all,

I want to take this code:

list_in_list = [[1,5,13,30,-4], [1,5,10,30,-4], [1,5,15,30,-4], [1,5,9,30,-4], [1,5,16,30,-8]]

and sort it by the 3rd item in each sublist (list_in_list[x][2])

I'd like to do it by the highest number so my output would be:

sorted_list_in_list = [[1,5,16,30,-8],[1,5,15,30,-4], [1,5,13,30,-4],[1,5,10,30,-4], [1,5,9,30,-4]]

Any help or suggestions?

Edited 7 Years Ago by rasizzle: n/a

It's difficult for me to translate the tuple and dictionary code to a simple list. Can anyone help explain it to me with my code?

Figured it out:

import operator

myreturn = operator.itemgetter(2)

list_in_list = [[1,5,13,30,-4], [1,5,10,30,-4], [1,5,15,30,-4], [1,5,9,30,-4], [1,5,16,30,-8]]

list_in_list.sort(key=myreturn, reverse=True)

print list_in_list
This question has already been answered. Start a new discussion instead.