Open In App

Python program to Sort Tuples by their Maximum element

Given a Tuple List sort tuples by maximum element in a tuple.

Input : test_list = [(4, 5, 5, 7), (1, 3, 7, 4), (19, 4, 5, 3), (1, 2)] 
Output : [(19, 4, 5, 3), (4, 5, 5, 7), (1, 3, 7, 4), (1, 2)] 
Explanation : 19 > 7 = 7 > 2, is order, hence reverse sorted by maximum element.



Input : test_list = [(4, 5, 5, 7), (19, 4, 5, 3), (1, 2)] 
Output : [(19, 4, 5, 3), (4, 5, 5, 7), (1, 2)] 
Explanation : 19 > 7 > 2, is order, hence reverse sorted by maximum element. 

Method #1 : Using max() + sort()



In this, we perform task of getting maximum element in tuple using max(), and sort() operation is used to perform sort.




# Python3 code to demonstrate working of
# Sort Tuples by Maximum element
# Using max() + sort()
 
# helper function
def get_max(sub):
    return max(sub)
 
# initializing list
test_list = [(4, 5, 5, 7), (1, 3, 7, 4), (19, 4, 5, 3), (1, 2)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# sort() is used to get sorted result
# reverse for sorting by max - first element's tuples
test_list.sort(key = get_max, reverse = True)
 
# printing result
print("Sorted Tuples : " + str(test_list))

Output:

The original list is : [(4, 5, 5, 7), (1, 3, 7, 4), (19, 4, 5, 3), (1, 2)] Sorted Tuples : [(19, 4, 5, 3), (4, 5, 5, 7), (1, 3, 7, 4), (1, 2)]

Time Complexity: O(n*nlogn) where n is the number of elements in the list “test_list”.  max() + sort() performs n*nlogn number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list

Method #2 : Using sort() + lambda + reverse

In this, we use similar functionality, the only difference here being use of lambda fnc. rather than external function for task of getting reverse sorting.




# Python3 code to demonstrate working of
# Sort Tuples by Maximum element
# Using sort() + lambda + reverse
 
# initializing list
test_list = [(4, 5, 5, 7), (1, 3, 7, 4), (19, 4, 5, 3), (1, 2)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# lambda function getting maximum elements
# reverse for sorting by max - first element's tuples
test_list.sort(key = lambda sub : max(sub), reverse = True)
 
# printing result
print("Sorted Tuples : " + str(test_list))

Output:

The original list is : [(4, 5, 5, 7), (1, 3, 7, 4), (19, 4, 5, 3), (1, 2)] Sorted Tuples : [(19, 4, 5, 3), (4, 5, 5, 7), (1, 3, 7, 4), (1, 2)]

Method #3: Using a loop to find the maximum element and sort based on it




# initializing list
test_list = [(4, 5, 5, 7), (1, 3, 7, 4), (19, 4, 5, 3), (1, 2)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# using a loop to find the maximum element and sort based on it
sorted_list = []
max_val = 0
for tup in test_list:
    max_val = max(tup)
    sorted_list.append((tup, max_val))
sorted_list.sort(key=lambda x: x[1], reverse=True)
final_list = [tup[0] for tup in sorted_list]
 
# printing result
print("Sorted Tuples : " + str(final_list))

Output
The original list is : [(4, 5, 5, 7), (1, 3, 7, 4), (19, 4, 5, 3), (1, 2)]
Sorted Tuples : [(19, 4, 5, 3), (4, 5, 5, 7), (1, 3, 7, 4), (1, 2)]

Time complexity: O(nlogn) – sorting takes O(nlogn) time, and the loop takes O(n) time, where n is the length of the input list.
Auxiliary space: O(n) – we are creating a new list with n tuples.

Method #6: Using Heapq module

Step-by-step approach:




import heapq
 
# initializing list
test_list = [(4, 5, 5, 7), (1, 3, 7, 4), (19, 4, 5, 3), (1, 2)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# using heapq to find the maximum element and sort based on it
max_values = []
for tup in test_list:
    max_values.append(heapq.nlargest(1, tup)[0])
new_list = list(zip(test_list, max_values))
sorted_list = sorted(new_list, key=lambda x: x[1], reverse=True)
final_list = [tup[0] for tup in sorted_list]
 
# printing result
print("Sorted Tuples : " + str(final_list))

Output
The original list is : [(4, 5, 5, 7), (1, 3, 7, 4), (19, 4, 5, 3), (1, 2)]
Sorted Tuples : [(19, 4, 5, 3), (4, 5, 5, 7), (1, 3, 7, 4), (1, 2)]

Time complexity: O(n log n), where n is the length of the test_list. 
Auxiliary space: O(n), where n is the length of the test_list.


Article Tags :