Python tuple program to sort a tuple by the maximum value of a tuple

In this python tuple program, we will sort a tuple by the maximum value of a tuple.

Steps to solve the program
  1. Take a list of tuples as input.
  2. 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().
  3. 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)]
				
			

remove nested elements from a tuple.

sort a list of tuples by the minimum value of a tuple.

Leave a Comment