![]() |
VOOZH | about |
Given Matrix, extract all the rows which has elements which have all elements which can be represented as dictionary key, i.e immutable.
Input : test_list = [[4, 5, [2, 3, 2]], ["gfg", 1, (4, 4)], [{5:4}, 3, "good"], [True, "best"]]
Output : [['gfg', 1, (4, 4)], [True, 'best']]
Explanation : All elements in tuples are immutable.Input : test_list = [[4, 5, [2, 3, 2]], ["gfg", 1, (4, 4), [3, 2]], [{5:4}, 3, "good"], [True, "best"]]
Output : [[True, 'best']]
Explanation : All elements in tuples are immutable.
Method #1 : Using all() + isinstance()
In this, we check for all elements to be of the instance of immutable data types, rows that return True for all elements, is filtered.
The original list is : [[4, 5, [2, 3, 2]], ['gfg', 1, (4, 4)], [{5: 4}, 3, 'good'], [True, 'best']]
Filtered rows : [['gfg', 1, (4, 4)], [True, 'best']]Time Complexity: O(n*m)
Auxiliary Space: O(k)
Method #2 : Using filter() + lambda + isinstance() + all()
In this, we perform task of filtering using filter() + lambda function, rest all functionalities are performed as above method.
The original list is : [[4, 5, [2, 3, 2]], ['gfg', 1, (4, 4)], [{5: 4}, 3, 'good'], [True, 'best']]
Filtered rows : [['gfg', 1, (4, 4)], [True, 'best']]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() + all() which all 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.
Method #3: Using itertools.filterfalse() method
The original list is : [[4, 5, [2, 3, 2]], ['gfg', 1, (4, 4)], [{5: 4}, 3, 'good'], [True, 'best']]
Filtered rows : [['gfg', 1, (4, 4)], [True, 'best']]Time Complexity: O(N*N)
Auxiliary Space: O(N*N)
Method #4 : Using all()+type() methods
The original list is : [[4, 5, [2, 3, 2]], ['gfg', 1, (4, 4)], [{5: 4}, 3, 'good'], [True, 'best']]
Filtered rows : [['gfg', 1, (4, 4)], [True, 'best']]Time Complexity: O(N*N)
Auxiliary Space: O(N*N)
Method 5: Filtering a nested list based on the types of its elements.
The idea is to filtering the rows of a nested list (test_list) such that only the rows whose elements are of specific types (int, bool, float, tuple, and str) are kept.
Below is the implementation:
The original list is : [[4, 5, [2, 3, 2]], ['gfg', 1, (4, 4)], [{5: 4}, 3, 'good'], [True, 'best']]
Filtered rows : [['gfg', 1, (4, 4)], [True, 'best']]Time complexity: O(n*m), where n is the number of rows in the nested list and m is the maximum number of elements in a row.
Auxiliary space: O(k), where k is the number of rows that pass the filter (i.e., the length of the filtered list res).
Method 6:Using numpy:
Algorithm :
Output:
The original list is : [[4, 5, [2, 3, 2]], ['gfg', 1, (4, 4)], [{5: 4}, 3, 'good'], [True, 'best']]
Filtered rows : [['gfg', 1, (4, 4)], [True, 'best']]
The time complexity : O(nm), where n is the number of rows in test_list and m is the maximum number of elements in a row. This is because we need to iterate over each row and each element in each row to determine if the row should be included in the filtered list.
The auxiliary space : O(k), where k is the number of valid rows in the filtered list. This is because we are creating a new list to store the filtered rows, which will be at most the same length as the input list. However, if all rows are valid, the space complexity could be O(nm) if we create a copy of the input list to store the filtered rows