- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to Sort Tuples by their Maximum element
When it is required to sort the tuples based on the maximum element in it, a method is defined that uses the ‘max’ method to return the highest element.
Next the ‘sort’ method can be used to sort the list based on the previously defined function.
Below is a demonstration of the same −
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)]
A method named ‘get_max_value’ is defined, that uses ‘max’ function to give the highest value.
A list of tuple is defined, and the elements are displayed on the console.
The list is sorted based on the key of previously defined function.
It is displayed in reverse order.
This is the output that is displayed on the console.
- Python program to sort tuples by frequency of their absolute difference
- Python - Sort Matrix by Maximum Row element
- Python – Sort by Maximum digit in Element
- Python program to sort tuples in increasing order by any key.
- Python program to sort a list of tuples by second Item
- Sort Tuples by Total digits in Python
- Python – Sort Tuples by Total digits
- Sort Tuples in Increasing Order by any key in Python program
- Python program to Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple
- Python program to sort a tuple by its float element
- Sort list of tuples by specific ordering in Python
- Python program to sort a list of tuples alphabetically
- Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple using Python program
- Python program to Sort a List of Dictionaries by the Sum of their Values
- How to group Python tuple elements by their first element?