[Tutor] Sorting a list of list

Stephen Nelson-Smith sanelson at gmail.com
Fri Jun 5 22:16:33 CEST 2015


As part of my league secretary program (to which thread I shall reply again
shortly), I need to sort a list of lists.  I've worked out that I can use
sorted() and operator.itemgetter to sort by a value at a known position in
each list.  Is it possible to do this at a secondary level?  So if the
items are the same, we use the secondary key?

Current function:

>>> def sort_table(table, col=0):
...     return sorted(table, key=operator.itemgetter(col), reverse=True)
...
>>> sort_table(results, 6)
[['spip', 2, 2, 0, 10, 0, 4], ['hpip', 2, 0, 2, 2, 8, 0]]

S.


More information about the Tutor mailing list