-5

I would like to sort this list of lists in descending order by the value of the seventh element in the lists:

unsorted_list = [["a", 5, 6, 3, 2, 4, 8], ["b", 5, 2, 7, 1, 10, 3], 
                 ["c", 2, 6, 3, 7, 11, 13]]

How can it be done? Thanks in advance!

  • 3
    Possible duplicate of [Sorting a list of lists in python](http://stackoverflow.com/questions/5201191/sorting-a-list-of-lists-in-python) – oz123 Nov 15 '15 at 19:04
  • @Pi, I think I could have answered this too, but in fact the OP shows lack of motivation to google this. This why this question should be closed. – oz123 Nov 15 '15 at 19:30

1 Answers1

0

Use itemgetter:

>> from operator import itemgetter
>> unsorted_list = [["a", 5, 6, 3, 2, 4, 8], ["b", 5, 2, 7, 1, 10, 3], 
             ["c", 2, 6, 3, 7, 11, 13]]
>> sorted(unsorted_list, key=itemgetter(6))
[['b', 5, 2, 7, 1, 10, 3], ['a', 5, 6, 3, 2, 4, 8], ['c', 2, 6, 3, 7, 11, 13]]
tylersimko
  • 193
  • 7