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
test_list = [[ 4 , 5 , 6 , 7 ], [], [], [ 9 , 8 , 1 ], []]
print ( "The original list is : " + str (test_list))
res = [row for row in test_list if len (row) > 0 ]
print ( "Filtered Matrix " + str (res))
|
Output
The 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
test_list = [[ 4 , 5 , 6 , 7 ], [], [], [ 9 , 8 , 1 ], []]
print ( "The original list is : " + str (test_list))
res = list ( filter ( lambda row: len (row) > 0 , test_list))
print ( "Filtered Matrix " + str (res))
|
Output
The 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
test_list = [[ 4 , 5 , 6 , 7 ], [], [], [ 9 , 8 , 1 ], []]
print ( "The original list is : " + str (test_list))
res = []
for i in test_list:
if str (i).find( "[]" ) = = - 1 :
res.append(i)
print ( "Filtered Matrix " + str (res))
|
Output
The 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
test_list = [[ 4 , 5 , 6 , 7 ], [], [], [ 9 , 8 , 1 ], []]
print ( "The original list is : " + str (test_list))
while ([] in test_list):
test_list.remove([])
print ( "Filtered Matrix " + str (test_list))
|
Output
The 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
test_list = [[ 4 , 5 , 6 , 7 ], [], [], [ 9 , 8 , 1 ], []]
print ( "The original list is : " + str (test_list))
i = 0
while i < len (test_list):
if not test_list[i]:
test_list.pop(i)
else :
i + = 1
print ( "Filtered Matrix: " + str (test_list))
|
Output
The 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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...