Given A Matrix, extract all the rows which have all the elements with particular data type.
Input : test_list = [[4, 5, "Hello"], [2, 6, 7], ["g", "f", "g"], [9, 10, 11]], data_type = int
Output : [[2, 6, 7], [9, 10, 11]]
Explanation : All lists with integer are extracted.
Input : test_list = [[4, 5, "Hello"], [2, 6, 7], ["g", "f", "g"], [9, 10, 11]], data_type = str
Output : [["g", "f", "g"]]
Explanation : All lists with strings are extracted.
The original list is : [[4, 5, 'Hello'], [2, 6, 7], ['g', 'f', 'g'], [9, 10, 11]] Filtered Rows : [[2, 6, 7], [9, 10, 11]]
The original list is : [[4, 5, 'Hello'], [2, 6, 7], ['g', 'f', 'g'], [9, 10, 11]] Filtered Rows : [[2, 6, 7], [9, 10, 11]]
Time Complexity: O(n), where n is the length of the input list. This is because we’re using the built-in filter() + lambda + isinstance() which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(n), where n is the length of the input list as we’re using additional space other than the input list itself.
OutputThe original list is : [[4, 5, 'Hello'], [2, 6, 7], ['g', 'f', 'g'], [9, 10, 11]]
Filtered Rows : [[2, 6, 7], [9, 10, 11]]