Friday, October 24, 2025
HomeLanguagesPython program to omit K length Rows

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 : [[4, 7], [8, 10, 12, 8], [10, 11]] 
Explanation : [6, 8, 10] omitted as length 3 rows. 
 

Method #1 :  Using loop + len()

In this, we check for the length of each row using len(), if found to be equal to K, that row is omitted from Matrix.

Python3




# Python3 code to demonstrate working of
# Omit K length Rows
# Using loop + len()
 
# initializing list
test_list = [[4, 7],
             [8, 10, 12, 8],
             [10, 11],
             [6, 8, 10]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 2
 
res = []
for row in test_list:
     
    # checking rows not K length
    if len(row) != K :
        res.append(row)
 
# printing result
print("Filtered Matrix : " + str(res))


 Output:

The original list is : [[4, 7], [8, 10, 12, 8], [10, 11], [6, 8, 10]] Filtered Matrix : [[8, 10, 12, 8], [6, 8, 10]]

Time Complexity: O(n*m)
Auxiliary Space: O(k)
 

Method #2 : Using filter() + lambda + len()

In this, we use filter() to extract rows with lengths other than K. The lambda function is used to render length computation logic.

Python3




# Python3 code to demonstrate working of
# Omit K length Rows
# Using filter() + lambda + len()
 
# initializing list
test_list = [[4, 7],
             [8, 10, 12, 8],
             [10, 11],
             [6, 8, 10]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 4
 
# getting elements with length other than K
res = list(filter(lambda row : len(row) != K, test_list))
 
# printing result
print("Filtered Matrix : " + str(res))


 Output:

The original list is : [[4, 7], [8, 10, 12, 8], [10, 11], [6, 8, 10]] Filtered Matrix : [[4, 7], [10, 11], [6, 8, 10]]

Time Complexity: O(n*m)
Auxiliary Space: O(k)

Using slicing and concatenation:

Approach:

Define a function named “omit_k_length_rows” that takes two arguments, a list of lists named “test_list” and an integer named “K”.
Create an empty list named “res” to store the filtered results.
Iterate through each list in “test_list” using a for loop.
Check if the length of the current list is not equal to “K” using the “len” function and the “!=” operator.
If the length is not equal to “K”, add the current list to the “res” list using the “+=” operator and list concatenation.
Return the filtered “res” list.
Use the function to solve the given input and print the output.

Python3




def omit_k_length_rows(test_list, K):
    res = []
    for row in test_list:
        if len(row) != K:
            res += [row]
    return res
test_list = [[4, 7], [8, 10, 12, 8], [10, 11], [6, 8, 10]]
K = 2
result = omit_k_length_rows(test_list, K)
print(result)  # Output: [[8, 10, 12, 8], [6, 8, 10]]
 
K = 3
result = omit_k_length_rows(test_list, K)
print(result)  # Output: [[4, 7], [8, 10, 12, 8], [10, 11]]


Output

[[8, 10, 12, 8], [6, 8, 10]]
[[4, 7], [8, 10, 12, 8], [10, 11]]

Time Complexity: O(n*m), where n is the number of rows and m is the maximum length of the rows
Auxiliary Space: O(n)

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

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS