- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

When it is required to sort matrix by row median, a method is defined that uses the ‘median’ method to determine the result.

Below is a demonstration of the same −

from statistics import median def median_row(row): return median(row) my_list = [[43, 14, 27], [13, 27, 24], [32, 56, 18], [34, 62, 55]] print("The list is :") print(my_list) my_list.sort(key = median_row) print("The result is :") print(my_list)

The list is : [[43, 14, 27], [13, 27, 24], [32, 56, 18], [34, 62, 55]] The result is : [[13, 27, 24], [43, 14, 27], [32, 56, 18], [34, 62, 55]]

The required packages are imported into the environment.

A method named ‘median_row’ is defined that takes row as a parameter, returns median of the row as output using ‘median’ method.

A list of 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.

This is the output that is displayed on the console.

- Related Questions & Answers
- Python - Sort Matrix by Maximum Row element
- Python – Sort row by K multiples
- Python – Sort Matrix by total characters
- Python – Sort Matrix by None frequency
- Python – Sort Matrix by Palindrome count
- Python – Sort Matrix by Maximum String Length
- How to divide matrix rows in R by row median?
- Python – Sort Matrix by K Sized Subarray Maximum Sum
- Python – Sort by range inclusion
- Python – Sort by Uppercase Frequency
- Python – Sort Dictionaries by Size
- Find median in row wise sorted matrix in C++
- Python – Sort Matrix by Number of elements greater than its previous element
- Python – Sort Tuples by Total digits
- Python – Sort Strings by Case difference

Advertisements