
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python - Sort Matrix by Maximum Row element
When it is required to sort matrix by maximum row element, a method is defined that takes one parameter and uses ‘max’ method to determine the result.
Example
Below is a demonstration of the same −
def sort_max(row): return max(row) my_list = [[15, 27, 18], [39, 20, 13], [13, 15, 56], [43, 13, 25]] print("The list is :") print(my_list) my_list.sort(key = sort_max, reverse = True) print("The result is :") print(my_list)
Output
The list is : [[15, 27, 18], [39, 20, 13], [13, 15, 56], [43, 13, 25]] The result is : [[13, 15, 56], [43, 13, 25], [39, 20, 13], [15, 27, 18]]
Explanation
A method named ‘sort_max’ is defined that takes row as a parameter, and returns maximum element of row as output.
Outside the method, a list is defined and displayed on the console.
The list is sorted using ‘sort’ method and the key is specified as the previously defined method.
In addition to this, the ‘reverse’ parameter in ‘sort’ method is set to ‘True’ so that the list is reverse sorted.
This is the output that is displayed on the console.
- Related Articles
- Python – Sort Matrix by Row Median
- Python – Sort Matrix by Maximum String Length
- Python – Sort by Maximum digit in Element
- Python – Sort Matrix by K Sized Subarray Maximum Sum
- Python program to Sort Tuples by their Maximum element
- Find the maximum element of each row in a matrix using Python
- Python – Sort row by K multiples
- Python Program to sort rows of a matrix by custom element count
- Python – Sort Matrix by Number of elements greater than its previous element
- Find maximum element of each row in a matrix in C++
- Python – Sort Matrix by total characters
- Python – Sort Matrix by None frequency
- Python – Sort Matrix by Palindrome count
- Sort the matrix row-wise and column-wise using Python
- JavaScript Program to Find maximum element of each row in a matrix

Advertisements