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.
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.
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]]
In this, we perform the task of filtering using filter() and lambda. len() is used for finding length of rows.
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]]
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]]
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.