Open In App

Python Program that prints the rows of a given length from a matrix

Last Updated : 28 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a Matrix, the following articles shows how to extract all the rows with a specified length.

Input : test_list = [[3, 4, 5, 6], [1, 4, 6], [2], [2, 3, 4, 5, 6], [7, 3, 1]], K = 3 
Output : [[1, 4, 6], [7, 3, 1]] 
Explanation : Extracted lists have length of 3.
Input : test_list = [[3, 4, 5, 6], [1, 4, 6], [2], [2, 3, 4, 5, 6], [7, 3, 1]], K = 4 
Output : [[3, 4, 5, 6]] 
Explanation : Extracted lists have length of 4. 

Method 1 : Using list comprehension and len()

In this, we perform the task of getting length using len() and list comprehension does the task of filtering all the rows which have a specified length.

Python3




# initializing list
test_list = [[3, 4, 5, 6], [1, 4, 6], [2], [2, 3, 4, 5, 6], [7, 3, 1]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 3
 
# list comprehension is used for extracting K len rows
res = [sub for sub in test_list if len(sub) == K]
 
# printing result
print("The filtered rows : " + str(res))


Output:

The original list is : [[3, 4, 5, 6], [1, 4, 6], [2], [2, 3, 4, 5, 6], [7, 3, 1]]

The filtered rows : [[1, 4, 6], [7, 3, 1]]

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

Method 2 : Using filter(), lambda and len()

In this, we perform the task of filtering using filter() and lambda. len() is used for finding length of rows.

Python3




# initializing list
test_list = [[3, 4, 5, 6], [1, 4, 6], [2], [2, 3, 4, 5, 6], [7, 3, 1]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 3
 
# filter() + lambda to filter out rows of len K
res = list(filter(lambda sub: len(sub) == K, test_list))
 
# printing result
print("The filtered rows : " + str(res))


Output:

The original list is : [[3, 4, 5, 6], [1, 4, 6], [2], [2, 3, 4, 5, 6], [7, 3, 1]]

The filtered rows : [[1, 4, 6], [7, 3, 1]]

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 3: Using a for loop

Python3




# initializing list
test_list = [[3, 4, 5, 6], [1, 4, 6], [2], [2, 3, 4, 5, 6], [7, 3, 1]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 3
 
# using for loop to filter out rows of len K
res = []
for sub in test_list:
    if len(sub) == K:
        res.append(sub)
 
# printing result
print("The filtered rows : " + str(res))
#This code is contributed by Vinay Pinjala.


Output

The original list is : [[3, 4, 5, 6], [1, 4, 6], [2], [2, 3, 4, 5, 6], [7, 3, 1]]
The filtered rows : [[1, 4, 6], [7, 3, 1]]

Time Complexity: O(n)

Auxiliary Space: O(n)

Method 4: Using the map() function

we can use the map() function along with a lambda function to create a new list containing only the sublists that have length K.

Python3




# initializing list
test_list = [[3, 4, 5, 6], [1, 4, 6], [2], [2, 3, 4, 5, 6], [7, 3, 1]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 3
 
# using map() and lambda to filter out rows of len K
res = list(filter(lambda x: len(x) == K, test_list))
 
# printing result
print("The filtered rows : " + str(res))


Output

The original list is : [[3, 4, 5, 6], [1, 4, 6], [2], [2, 3, 4, 5, 6], [7, 3, 1]]
The filtered rows : [[1, 4, 6], [7, 3, 1]]

The time complexity of this method is also O(n), where n is the number of sublists in the original list.

The auxiliary space required for this method is O(1), as it does not create any new lists.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads