Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to Sort Tuples by their Maximum element
When it is required to sort tuples based on their maximum element, we can define a function that uses the max() method to find the highest element in each tuple. The sort() method can then use this function as a key to sort the list of tuples.
Method 1: Using Custom Function with sort()
We can create a helper function to extract the maximum value from each tuple ?
def get_max_value(my_val):
return max(my_val)
my_list = [(4, 6, 8, 1), (13, 21, 42, 56), (7, 1, 9, 0), (1, 2)]
print("The list is :")
print(my_list)
my_list.sort(key=get_max_value, reverse=True)
print("The sorted tuples are :")
print(my_list)
The list is : [(4, 6, 8, 1), (13, 21, 42, 56), (7, 1, 9, 0), (1, 2)] The sorted tuples are : [(13, 21, 42, 56), (7, 1, 9, 0), (4, 6, 8, 1), (1, 2)]
Method 2: Using Lambda Function
A more concise approach uses a lambda function instead of defining a separate function ?
my_list = [(4, 6, 8, 1), (13, 21, 42, 56), (7, 1, 9, 0), (1, 2)]
print("Original list:")
print(my_list)
# Sort in descending order by maximum element
my_list.sort(key=lambda x: max(x), reverse=True)
print("Sorted tuples (descending):")
print(my_list)
# Sort in ascending order
my_list.sort(key=lambda x: max(x))
print("Sorted tuples (ascending):")
print(my_list)
Original list: [(4, 6, 8, 1), (13, 21, 42, 56), (7, 1, 9, 0), (1, 2)] Sorted tuples (descending): [(13, 21, 42, 56), (7, 1, 9, 0), (4, 6, 8, 1), (1, 2)] Sorted tuples (ascending): [(1, 2), (4, 6, 8, 1), (7, 1, 9, 0), (13, 21, 42, 56)]
Method 3: Using sorted() Function
The sorted() function creates a new sorted list without modifying the original ?
my_list = [(4, 6, 8, 1), (13, 21, 42, 56), (7, 1, 9, 0), (1, 2)]
print("Original list:")
print(my_list)
# Create new sorted list
sorted_list = sorted(my_list, key=max, reverse=True)
print("Sorted tuples:")
print(sorted_list)
print("Original list unchanged:")
print(my_list)
Original list: [(4, 6, 8, 1), (13, 21, 42, 56), (7, 1, 9, 0), (1, 2)] Sorted tuples: [(13, 21, 42, 56), (7, 1, 9, 0), (4, 6, 8, 1), (1, 2)] Original list unchanged: [(4, 6, 8, 1), (13, 21, 42, 56), (7, 1, 9, 0), (1, 2)]
How It Works
The
max()function finds the largest element in each tupleThe
keyparameter tells the sorting function which value to use for comparisonSetting
reverse=Truesorts in descending order (largest maximum first)The
sort()method modifies the original list, whilesorted()returns a new list
Comparison
| Method | Modifies Original? | Best For |
|---|---|---|
list.sort() |
Yes | In-place sorting |
sorted() |
No | Creating new sorted list |
| Lambda function | Depends on method | Concise one-line sorting |
Conclusion
Use lambda x: max(x) for concise sorting by maximum element. Choose sort() for in-place modification or sorted() to preserve the original list. Set reverse=True for descending order.
