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
# 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.
OutputThe 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))
OutputThe 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.
Similar Reads
Python Program that prints rows from the matrix that have same element at a given index Given a Matrix, the following article shows how rows which has similar digit at the specified index will be extracted and returned as output. Input : test_list = [[3345, 6355, 83, 938], [323, 923, 845], [192, 993, 49], [98, 34, 23]], K = 1 Output : [[3345, 6355, 83, 938], [192, 993, 49]] Explanation
5 min read
Python Program to Print a given matrix in reverse spiral form Given a 2D array, print it in reverse spiral form. We have already discussed Print a given matrix in spiral form. This article discusses how to do the reverse printing. See the following examples. Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 10 11 7 6 5 9 13 14 15 16 12 8 4 3 2 1 Input: 1 2
5 min read
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 emp
4 min read
Python Program to sort rows of a matrix by custom element count Given Matrix, the following program shows how to sort rows of a matrix by the count of presence of numbers from a specified list. Input : test_list = [[4, 5, 1, 7], [6, 5], [9, 8, 2], [7, 1]], cus_list = [4, 5, 7] Output : [[9, 8, 2], [6, 5], [7, 1], [4, 5, 1, 7]] Explanation : 0 < 1 = 1 < 3 i
5 min read
Python Program to Print matrix in antispiral form Given a 2D array, the task is to print matrix in anti spiral form:Examples: Output: 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 Input : arr[][4] = {1, 2, 3, 4 5, 6, 7, 8 9, 10, 11, 12 13, 14, 15, 16}; Output : 10 11 7 6 5 9 13 14 15 16 12 8 4 3 2 1 Input :arr[][6] = {1, 2, 3, 4, 5, 6 7, 8, 9, 10, 11, 12
2 min read
Python program to print Rows where all its Elements' frequency is greater than K Given Matrix, extract all rows whose all elements have a frequency greater than K. Input : test_list = [[1, 1, 2, 3, 2, 3], [4, 4, 5, 6, 6], [1, 1, 1, 1], [4, 5, 6, 8]], K = 2 Output : [[1, 1, 1, 1]] Explanation : Here, frequency of 1 is 4 > 2, hence row retained. Input : test_list = [[1, 1, 2, 3
8 min read
Python Program to test whether the length of rows are in increasing order Given a Matrix, the following program is used to test if length all rows of a matrix are in increasing order or not. If yes, it returns True, otherwise False. Input : test_list = [[3], [1, 7], [10, 2, 4], [8, 6, 5, 1, 4]] Output : True Explanation : 1 < 2 < 3 < 5, increasing lengths of rows
4 min read
Python Program for Print matrix in zag-zag fashion Given a matrix of 2D array of n rows and m columns. Print this matrix in ZIG-ZAG fashion as shown in figure. Example: Input: 1 2 3 4 5 6 7 8 9 Output: 1 2 4 7 5 3 6 8 9 Method 1:Approach of Python3 code This approach is simple. While travelling the matrix in the usual fashion, on basis of parity of
3 min read
Python Program to find transpose of a matrix Transpose of a matrix is obtained by changing rows to columns and columns to rows. In other words, transpose of A[][] is obtained by changing A[i][j] to A[j][i]. For Square Matrix: The below program finds transpose of A[][] and stores the result in B[][], we can change N for different dimension. Pyt
5 min read