- 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 Program that filters out non-empty rows of a matrix

When it is required to filter out non-empty rows from a matrix, a simple list comprehension along with the ‘len’ method can be used.

Below is a demonstration of the same −

## Example

my_list = [[21, 52, 4, 74], [], [7, 8, 4, 1], [], []] print("The list is :") print(my_list) my_result = [row for row in my_list if len(row) > 0] print("The resultant list is :") print(my_result)

## Output

The list is : [[21, 52, 4, 74], [], [7, 8, 4, 1], [], []] The resultant list is : [[21, 52, 4, 74], [7, 8, 4, 1]]

## Explanation

A list of list with integers is defined and is displayed on the console.

The list is iterated over using list comprehension.

It checks if the length of an element is greater than 0 or not.

If yes, it is stored in the list.

Otherwise, it is ignored.

This is assigned to a variable.

This variable is displayed as output on the console.

- Related Questions & Answers
- Python Program that prints the rows of a given length from a matrix
- Python program to extract rows from Matrix that has distinct data types
- Python Program to find out the determinant of a given special matrix
- Python Program to sort rows of a matrix by custom element count
- Python Program to Extract Rows of a matrix with Even frequency Elements
- Python program to sort matrix based upon sum of rows
- Python Program to Remove the nth Index Character from a Non-Empty String
- Count rows in a matrix that consist of same element in C++
- Write Python program to find duplicate rows in a binary matrix
- Program to find maximum non negative product in a matrix in Python
- Python - Print rows from the matrix that have same element at a given index
- First Non-Empty String in list in Python
- Python Program to Sort Matrix Rows by summation of consecutive difference of elements
- Program to check how many ways we can choose empty cells of a matrix in python
- How to select rows of an R data frame that are non-NA?

Advertisements