- 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

# Python – Sort Matrix by Row Median

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 −

## Example

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)

## Output

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]]

## Explanation

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
- How to divide matrix rows in R by row median?
- Python – Sort row by K multiples
- Find median in row wise sorted matrix in C++
- 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 the data frame row values in R by row median?
- How to divide data.table object rows by row median in R?
- Python – Sort Matrix by K Sized Subarray Maximum Sum
- How to sort a boxplot by the median values in Pandas?
- How to find the row median of columns having same name in R matrix?
- How to divide the row values by row mean in R matrix?
- How to divide the row values by row sum in R matrix?

Advertisements