Python – Extract rows with Complex data types

Given Matrix, extract rows with complex data types.
Examples:
Input : test_list = [[4, 2, 5], [1, [3, 4], 9], [5], [7, (2, 3), 3, 9]]
Output : [[1, [3, 4], 9], [7, (2, 3), 3, 9]]
Explanation : Rows have lists and tuples respectively.
Input : test_list = [[4, 2, [5]], [1, [3, 4], 9], [5], [7, (2, 3), 3, 9]]
Output : [[4, 2, [5]], [1, [3, 4], 9], [7, (2, 3), 3, 9]]
Explanation : Rows have lists and tuples respectively.
Method #1: Using list comprehension + isinstance() + any()
In this, we check for each element of row to be of dictionary, tuple, set or list datatype using isinstance(), if any element is found to have that instance, the row is added in result.
Python3
# Python3 code to demonstrate working of# Extract rows with Complex data types# Using list comprehension + isinstance() + any()# initializing listtest_list = [[4, 2, 5], [1, [3, 4], 9], [5], [7, (2, 3), 3, 9]]# printing original listprint("The original list is : " + str(test_list))# Checking for any of list, set, tuple or# dictionary as complex structuresres = [row for row in test_list if any(isinstance(ele, list) or isinstance(ele, tuple) or isinstance(ele, dict) or isinstance(ele, set) for ele in row)]# printing resultprint("Filtered Rows : " + str(res)) |
The original list is : [[4, 2, 5], [1, [3, 4], 9], [5], [7, (2, 3), 3, 9]] Filtered Rows : [[1, [3, 4], 9], [7, (2, 3), 3, 9]]
Time Complexity: O(n2*m2)
Auxiliary Space: O(k)
Method #2 : Using filter() + lambda + isinstance()
In this, we perform task of filtering using filter and lambda, checking for data type is done using isinstance().
Python3
# Python3 code to demonstrate working of# Extract rows with Complex data types# Using filter() + lambda + isinstance()# initializing listtest_list = [[4, 2, 5], [1, [3, 4], 9], [5], [7, (2, 3), 3, 9]]# printing original listprint("The original list is : " + str(test_list))# checking for any of list, set, tuple or dictionary as complex structuresres = list(filter(lambda row: any(isinstance(ele, list) or isinstance(ele, tuple) or isinstance(ele, dict) or isinstance(ele, set) for ele in row), test_list))# printing resultprint("Filtered Rows : " + str(res)) |
The original list is : [[4, 2, 5], [1, [3, 4], 9], [5], [7, (2, 3), 3, 9]] Filtered Rows : [[1, [3, 4], 9], [7, (2, 3), 3, 9]]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3 : Using filter()+lambda+type()
Python3
# Python3 code to demonstrate working of# Extract rows with Complex data types# Using filter() + lambda + type()# Initializing listtest_list = [[4, 2, 5], [1, [3, 4], 9], [5], [7, (2, 3), 3, 9]]# Printing original listprint("The original list is : " + str(test_list))# Checking for any of list, set, tuple or dictionary# as complex structuresres = list(filter(lambda row: any(type(ele) is list or type(ele) is tuple or type(ele) is dict or type(ele) is set for ele in row), test_list))# Printing resultprint("Filtered Rows : " + str(res)) |
The original list is : [[4, 2, 5], [1, [3, 4], 9], [5], [7, (2, 3), 3, 9]] Filtered Rows : [[1, [3, 4], 9], [7, (2, 3), 3, 9]]
Time Complexity: O(N), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(N), where n is the number of elements in the list “test_list”.
Method #4: Using a for loop and isinstance()
Steps:
- Initialize an empty list to store the filtered rows.
- Iterate over each row in the input list using a for loop.
- Use any() function along with isinstance() to check if any element in the current row is of a complex data type.
- If the condition is true, append the current row to the filtered list.
- Return the filtered list.
Example:
Python3
# Python3 code to demonstrate working of# Extract rows with Complex data types# Using for loop + isinstance()# initializing listtest_list = [[4, 2, 5], [1, [3, 4], 9], [5], [7, (2, 3), 3, 9]]# printing original listprint("The original list is : " + str(test_list))# initializing empty list to store filtered rowsfiltered_rows = []# iterating over each row in the input listfor row in test_list: # checking if any element in the row is of complex data type if any(isinstance(ele, (list, tuple, dict, set)) for ele in row): # appending the current row to the filtered list filtered_rows.append(row)# printing resultprint("Filtered Rows : " + str(filtered_rows)) |
The original list is : [[4, 2, 5], [1, [3, 4], 9], [5], [7, (2, 3), 3, 9]] Filtered Rows : [[1, [3, 4], 9], [7, (2, 3), 3, 9]]
Time complexity: O(n*m) where n is the number of rows and m is the maximum number of elements in any row.
Auxiliary space: O(k) where k is the number of filtered rows.



