Thursday, September 4, 2025
HomeLanguagesPython Program that filters out non-empty rows of a matrix

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


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




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


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




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


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




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


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




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


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.

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6629 POSTS0 COMMENTS
Nicole Veronica
11799 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11859 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6698 POSTS0 COMMENTS
Umr Jansen
6717 POSTS0 COMMENTS