Python Program that prints the rows of a given length from a matrix
Last Updated :
28 Apr, 2023
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
test_list = [[ 3 , 4 , 5 , 6 ], [ 1 , 4 , 6 ], [ 2 ], [ 2 , 3 , 4 , 5 , 6 ], [ 7 , 3 , 1 ]]
print ( "The original list is : " + str (test_list))
K = 3
res = [sub for sub in test_list if len (sub) = = K]
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
test_list = [[ 3 , 4 , 5 , 6 ], [ 1 , 4 , 6 ], [ 2 ], [ 2 , 3 , 4 , 5 , 6 ], [ 7 , 3 , 1 ]]
print ( "The original list is : " + str (test_list))
K = 3
res = list ( filter ( lambda sub: len (sub) = = K, test_list))
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
test_list = [[ 3 , 4 , 5 , 6 ], [ 1 , 4 , 6 ], [ 2 ], [ 2 , 3 , 4 , 5 , 6 ], [ 7 , 3 , 1 ]]
print ( "The original list is : " + str (test_list))
K = 3
res = []
for sub in test_list:
if len (sub) = = K:
res.append(sub)
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 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
test_list = [[ 3 , 4 , 5 , 6 ], [ 1 , 4 , 6 ], [ 2 ], [ 2 , 3 , 4 , 5 , 6 ], [ 7 , 3 , 1 ]]
print ( "The original list is : " + str (test_list))
K = 3
res = list ( filter ( lambda x: len (x) = = K, test_list))
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.
Share your thoughts in the comments
Please Login to comment...