Answer the question
In order to leave comments, you need to log in
How to sort a two dimensional list by two columns in python 3?
How to sort by one column is clear:
student_tuples = [
('john', 'A', 15),
('jane', 'B', 12),
('dave', 'B', 10),
]
student_tuples.sort(key=lambda student: student[2])
Answer the question
In order to leave comments, you need to log in
right in the forehead without thinking much
student_tuples = [
... ('john', 'A', 15),
... ('jane', 'B', 12),
... ('dave', 'B', 10),
... ('dave1', 'B', 11),
... ('dave2', 'B', 1),
... ]
... student_tuples.sort(key=lambda student: (student[1], student[2]))
student_tuples
[('john', 'A', 15), ('dave2', 'B', 1), ('dave', 'B', 10), ('dave1', 'B', 11), ('jane', 'B', 12)]
The method sort
in Python uses stablesort , so you can sort twice - first on one field, then on the other.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question