Python Program that filters out non-empty rows of a matrix

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 listtest_list = [[4, 5, 6, 7], [], [], [9, 8, 1], []]# printing original listsprint("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 resultprint("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.
Python3
# Initializing listtest_list = [[4, 5, 6, 7], [], [], [9, 8, 1], []]# Printing original listsprint("The original list is : " + str(test_list))# Checking for row lengths # using len() filtering using filter() + lambdares = list(filter(lambda row: len(row) > 0, test_list))# Printing resultprint("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
Python3
#Filter non empty rows# Initializing listtest_list = [[4, 5, 6, 7], [], [], [9, 8, 1], []]# Printing original listsprint("The original list is : " + str(test_list))# Empty list res=[]for i in test_list: if str(i).find("[]")==-1: res.append(i)# Printing resultprint("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
Python3
#Python Program that filters out non-empty rows of a matrix# initializing listtest_list = [[4, 5, 6, 7], [], [], [9, 8, 1], []]# printing original listsprint("The original list is : " + str(test_list))while([] in test_list): test_list.remove([])# printing resultprint("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
Python3
# Initializing listtest_list = [[4, 5, 6, 7], [], [], [9, 8, 1], []]# Printing original listsprint("The original list is : " + str(test_list))# Removing empty rows using while loopi = 0while i < len(test_list): if not test_list[i]: test_list.pop(i) else: i += 1# Printing resultprint("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.



