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

 `# 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

 `# 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

• Initialize an empty list “sorted_list”.
• Initialize a variable “max_val” to 0.
• Loop through each tuple in the original list “test_list”.
• Within the loop, find the maximum value in the tuple using the max() function and assign it to “max_val”.
• Append a tuple of the original tuple and “max_val” to “sorted_list”.
• Sort “sorted_list” in descending order based on the “max_val” value in each tuple.
• Create a new list “final_list” with just the original tuples from “sorted_list”.
• Print “final_list”.

## Python3

 `# 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 the heapq module.
• Initialize an empty list called max_values.
• Use a for loop to iterate over each tuple in the test_list.
• Use the nlargest() function from the heapq module to find the maximum value in the tuple.
• Append the maximum value to the max_values list.
• Use the zip() function to combine the test_list and max_values list into a new list of tuples.
• Use the sorted() function to sort the new list of tuples based on the second element (i.e., the maximum value).
• Use a list comprehension to extract the first element of each tuple in the sorted list and assign it to a variable called final_list.
• Print the final_list.

## Python3

 `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.