Python Program that filters out non-empty rows of a matrix
Last Updated :
15 May, 2023
Given Matrix, the following article shows how to filter all the Non-Empty rows of a matrix. In simpler terms, the codes provided below return a matrix after removing empty rows from it.
Input : test_list = [[4, 5, 6, 7], [], [], [9, 8, 1], []]
Output : [[4, 5, 6, 7], [9, 8, 1]]
Explanation : All empty rows are removed.
Input : test_list = [[4, 5, 6, 7], [], [9, 8, 1], []]
Output : [[4, 5, 6, 7], [9, 8, 1]]
Explanation : All empty rows are removed.
Method 1: Using list comprehension and len()
In this we check each row for its length, if its length is greater than 0 then that row is added to result.
Python3
# initializing list
test_list = [[4, 5, 6, 7], [], [], [9, 8, 1], []]
# printing original lists
print("The original list is : " + str(test_list))
# checking for row lengths using len()
res = [row for row in test_list if len(row) > 0]
# printing result
print("Filtered Matrix " + str(res))
OutputThe original list is : [[4, 5, 6, 7], [], [], [9, 8, 1], []]
Filtered Matrix [[4, 5, 6, 7], [9, 8, 1]]
Time Complexity: O(n*m)
Auxiliary Space: O(k)
Method 2 : Using filter(), lambda and len()
In this, we filter rows w.r.t lengths using filter() and lambda function. The len() is used to get the length.
Python3
# Initializing list
test_list = [[4, 5, 6, 7], [], [], [9, 8, 1], []]
# Printing original lists
print("The original list is : " + str(test_list))
# Checking for row lengths
# using len() filtering using filter() + lambda
res = list(filter(lambda row: len(row) > 0, test_list))
# Printing result
print("Filtered Matrix " + str(res))
OutputThe original list is : [[4, 5, 6, 7], [], [], [9, 8, 1], []]
Filtered Matrix [[4, 5, 6, 7], [9, 8, 1]]
Time Complexity: O(n) where n is the number of elements in the list “test_list”. The time complexity of the filter() and lambda function is O(n)
Auxiliary Space: O(1), no extra space is required
Method 3 : Using find() method
Python3
#Filter non empty rows
# Initializing list
test_list = [[4, 5, 6, 7], [], [], [9, 8, 1], []]
# Printing original lists
print("The original list is : " + str(test_list))
# Empty list
res=[]
for i in test_list:
if str(i).find("[]")==-1:
res.append(i)
# Printing result
print("Filtered Matrix " + str(res))
OutputThe original list is : [[4, 5, 6, 7], [], [], [9, 8, 1], []]
Filtered Matrix [[4, 5, 6, 7], [9, 8, 1]]
Method 4 : Using remove() method
Python3
#Python Program that filters out non-empty rows of a matrix
# initializing list
test_list = [[4, 5, 6, 7], [], [], [9, 8, 1], []]
# printing original lists
print("The original list is : " + str(test_list))
while([] in test_list):
test_list.remove([])
# printing result
print("Filtered Matrix " + str(test_list))
OutputThe original list is : [[4, 5, 6, 7], [], [], [9, 8, 1], []]
Filtered Matrix [[4, 5, 6, 7], [9, 8, 1]]
Method 5 : Using pop() method
Python3
# Initializing list
test_list = [[4, 5, 6, 7], [], [], [9, 8, 1], []]
# Printing original lists
print("The original list is : " + str(test_list))
# Removing empty rows using while loop
i = 0
while i < len(test_list):
if not test_list[i]:
test_list.pop(i)
else:
i += 1
# Printing result
print("Filtered Matrix: " + str(test_list))
OutputThe original list is : [[4, 5, 6, 7], [], [], [9, 8, 1], []]
Filtered Matrix: [[4, 5, 6, 7], [9, 8, 1]]
Time complexity: O(nm), where n is the number of rows and m is the maximum number of elements in a row.
Auxiliary space O(1), since we are modifying the original list in place without creating any additional data structures.
Similar Reads
Python Program that prints the rows of a given length from a matrix
Given a Matrix, the following articles shows how to extract all the rows with a specified length. Input : test_list = [[3, 4, 5, 6], [1, 4, 6], [2], [2, 3, 4, 5, 6], [7, 3, 1]], K = 3 Output : [[1, 4, 6], [7, 3, 1]] Explanation : Extracted lists have length of 3.Input : test_list = [[3, 4, 5, 6], [1
4 min read
Python Program to Check if a given matrix is sparse or not
A matrix is a two-dimensional data object having m rows and n columns, therefore a total of m*n values. If most of the values of a matrix are 0 then we say that the matrix is sparse. Consider a definition of Sparse where a matrix is considered sparse if the number of 0s is more than half of the elem
4 min read
Python Program to Extract Rows of a matrix with Even frequency Elements
Given a Matrix, the task is to write a Python program to extract all the rows which have even frequencies of elements. Examples: Input: [[4, 5, 5, 2], [4, 4, 4, 4, 2, 2], [6, 5, 6, 5], [1, 2, 3, 4]] Output: [[4, 4, 4, 4, 2, 2], [6, 5, 6, 5]]Explanation: frequency of 4-> 4 which is even frequency
5 min read
Python - Filter rows with Elements as Multiple of K
Given a Matrix, extract rows with elements multiple of K. Input : test_list = [[5, 10, 15], [4, 8, 12], [100, 15], [5, 10, 23]], K = 4 Output : [[4, 8, 12]] Explanation : All are multiples of 4. Input : test_list = [[5, 10, 15], [4, 8, 11], [100, 15], [5, 10, 23]], K = 4 Output : [] Explanation : No
6 min read
Python program to Convert a Matrix to Sparse Matrix
Converting a matrix to a sparse matrix involves storing only non-zero elements along with their row and column indices to save memory.Using a DictionaryConverting a matrix to a sparse matrix using a dictionary involves storing only the non-zero elements of the matrix, with their row and column indic
2 min read
Python program to omit K length Rows
Given a Matrix, remove rows with length K. Input : test_list = [[4, 7], [8, 10, 12, 8], [10, 11], [6, 8, 10]], K = 2 Output : [[8, 10, 12, 8], [6, 8, 10]] Explanation : [4, 7] and [10, 11] omitted as length 2 rows. Input : test_list = [[4, 7], [8, 10, 12, 8], [10, 11], [6, 8, 10]], K = 3 Output : [[
3 min read
Python program to remove rows with duplicate element in Matrix
Given Matrix, remove all rows which have duplicate elements in them. Input : test_list = [[4, 3, 2], [7, 6, 7], [2, 4, 4], [8, 9, 9]] Output : [[4, 3, 2]] Explanation : [4, 3, 2] is the only unique row. Input : test_list = [[4, 3, 3, 2], [7, 6, 7], [2, 4, 4], [8, 9, 9]] Output : [] Explanation : No
5 min read
Python program to extract rows from Matrix that has distinct data types
Given a Matrix, the task is to write a Python program to extract rows with no repeated data types. Examples: Input : test_list = [[4, 3, 1], ["gfg", 3, {4:2}], [3, 1, "jkl"], [9, (2, 3)]]Output : [['gfg', 3, {4: 2}], [9, (2, 3)]]Explanation : [4, 3, 1] are all integers hence omitted. [9, (2, 3)] has
6 min read
Python Program that prints rows from the matrix that have same element at a given index
Given a Matrix, the following article shows how rows which has similar digit at the specified index will be extracted and returned as output. Input : test_list = [[3345, 6355, 83, 938], [323, 923, 845], [192, 993, 49], [98, 34, 23]], K = 1 Output : [[3345, 6355, 83, 938], [192, 993, 49]] Explanation
5 min read
Python program to print Rows where all its Elements' frequency is greater than K
Given Matrix, extract all rows whose all elements have a frequency greater than K. Input : test_list = [[1, 1, 2, 3, 2, 3], [4, 4, 5, 6, 6], [1, 1, 1, 1], [4, 5, 6, 8]], K = 2 Output : [[1, 1, 1, 1]] Explanation : Here, frequency of 1 is 4 > 2, hence row retained. Input : test_list = [[1, 1, 2, 3
8 min read