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.
# 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))
|
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.
# 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))
|
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
#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))
|
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
#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))
|
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
# 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))
|
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.