In this python tuple program, we will sort a tuple by the maximum value of a tuple.
Steps to solve the program
- Take a list of tuples as input.
- Use sorted() to sort the tuple and pass the lambda function to sort the tuple by the maximum value to the key inside the sorted().
- Print the output.
l = [ (1, 5, 7), (3, 4, 2), (4, 9, 0) ]
print("Original list of tuples: ",l)
l1 = sorted(l,key = lambda x: max(x),reverse=True)
print("After sorting: ",l1)
Output :
Original list of tuples: [(1, 5, 7), (3, 4, 2), (4, 9, 0)]
After sorting: [(4, 9, 0), (1, 5, 7), (3, 4, 2)]