- 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 program to sort matrix based upon sum of rows

When it is required to sort matrix based upon sum of rows, a method is defined that uses ‘sum’ method to determine the result.

Below is a demonstration of the same −

## Example

def sort_sum(row): return sum(row) my_list = [[34, 51], [32, 15, 67], [12, 41], [54, 36, 22]] print("The list is :") print(my_list) my_list.sort(key = sort_sum) print("The result is :") print(my_list)

## Output

The list is : [[34, 51], [32, 15, 67], [12, 41], [54, 36, 22]] The result is : [[12, 41], [34, 51], [54, 36, 22], [32, 15, 67]]

## Explanation

A method named ‘sort_sum’ is defined that takes a list as a parameter, and returns sum of the elements of the list as output.

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 Articles
- Python Program to sort rows of a matrix by custom element count
- Python Program to Sort Matrix Rows by summation of consecutive difference of elements
- C program to sort all columns and rows of matrix
- Swift Program to calculate the sum of rows of matrix elements
- Golang Program to calculate the sum of rows of matrix elements
- Program to find matrix for which rows and columns holding sum of behind rows and columns in Python
- Python – Sort Matrix by K Sized Subarray Maximum Sum
- Python program to sort table based on given attribute index
- Python program to remove rows with duplicate element in Matrix
- Python Program to Extract Rows of a matrix with Even frequency Elements
- Program to find diagonal sum of a matrix in Python
- Program to sort out phrases based on their appearances in Python
- How can Tensorflow be used to sum specific elements/rows of a matrix in Python?
- Write Python program to find duplicate rows in a binary matrix
- Python Program that filters out non-empty rows of a matrix

Advertisements