Python – Extract rows with Complex data types
Last Updated :
04 May, 2023
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
test_list = [[ 4 , 2 , 5 ], [ 1 , [ 3 , 4 ], 9 ], [ 5 ], [ 7 , ( 2 , 3 ), 3 , 9 ]]
print ( "The original list is : " + str (test_list))
res = [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)]
print ( "Filtered Rows : " + str (res))
|
Output
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
test_list = [[ 4 , 2 , 5 ], [ 1 , [ 3 , 4 ], 9 ], [ 5 ], [ 7 , ( 2 , 3 ), 3 , 9 ]]
print ( "The original list is : " + str (test_list))
res = 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))
print ( "Filtered Rows : " + str (res))
|
Output
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
test_list = [[ 4 , 2 , 5 ], [ 1 , [ 3 , 4 ], 9 ], [ 5 ], [ 7 , ( 2 , 3 ), 3 , 9 ]]
print ( "The original list is : " + str (test_list))
res = 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))
print ( "Filtered Rows : " + str (res))
|
Output
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
test_list = [[ 4 , 2 , 5 ], [ 1 , [ 3 , 4 ], 9 ], [ 5 ], [ 7 , ( 2 , 3 ), 3 , 9 ]]
print ( "The original list is : " + str (test_list))
filtered_rows = []
for row in test_list:
if any ( isinstance (ele, ( list , tuple , dict , set )) for ele in row):
filtered_rows.append(row)
print ( "Filtered Rows : " + str (filtered_rows))
|
Output
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.
Share your thoughts in the comments
Please Login to comment...