You can sort a list of tuple of tuples in Python using the sorted()
method along with a lambda function.
Here's an example:
main.py139 chars5 lines
Output: [(1, 5, 2), (2, 2, 2), (3, 1, 1), (3, 2, 6)]
The key
parameter in the sorted()
method takes a lambda function which specifies the criteria for the sorting.
In this example, the lambda function returns a tuple of the elements in each tuple of tuples to be sorted by their index.
Therefore, the outer list of tuples is sorted first by the first element, then by the second element and finally by the third element.
gistlibby LogSnag