I am trying to use a quick sort to sort through a list of lists at index [1]. For example:
list = [[2, 5, 3],
[2, 4, 9],
[0, 9, 1],
[1, 1
You want to use the key
argument in list.sort
:
import operator
mylist = [[2, 5, 3],
[2, 4, 9],
[0, 9, 1],
[1, 1, 1],
[4, 7, 5]]
mylist.sort(key=operator.itemgetter(1))
Output:
>>> mylist = [[2, 5, 3],
... [2, 4, 9],
... [0, 9, 1],
... [1, 1, 1],
... [4, 7, 5]]
>>>
>>> mylist.sort(key=operator.itemgetter(1))
>>> mylist
[[1, 1, 1], [2, 4, 9], [2, 5, 3], [4, 7, 5], [0, 9, 1]]