Open In App

Python – Extract rows with Complex data types

Last Updated : 04 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 list
test_list = [[4, 2, 5], [1, [3, 4], 9], [5], [7, (2, 3), 3, 9]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Checking for any of list, set, tuple or
# dictionary as complex structures
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)]
 
# printing result
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




# Python3 code to demonstrate working of
# Extract rows with Complex data types
# Using filter() + lambda + isinstance()
 
# initializing list
test_list = [[4, 2, 5], [1, [3, 4], 9], [5], [7, (2, 3), 3, 9]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# checking for any of list, set, tuple or dictionary as complex structures
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))
 
# printing result
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




# Python3 code to demonstrate working of
# Extract rows with Complex data types
# Using filter() + lambda + type()
 
# Initializing list
test_list = [[4, 2, 5], [1, [3, 4], 9], [5], [7, (2, 3), 3, 9]]
 
# Printing original list
print("The original list is : " + str(test_list))
 
# Checking for any of list, set, tuple or dictionary
# as complex structures
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))
 
# Printing result
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:

  1. Initialize an empty list to store the filtered rows.
  2. Iterate over each row in the input list using a for loop.
  3. Use any() function along with isinstance() to check if any element in the current row is of a complex data type.
  4. If the condition is true, append the current row to the filtered list.
  5. Return the filtered list.

Example:

Python3




# Python3 code to demonstrate working of
# Extract rows with Complex data types
# Using for loop + isinstance()
 
# initializing list
test_list = [[4, 2, 5], [1, [3, 4], 9], [5], [7, (2, 3), 3, 9]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing empty list to store filtered rows
filtered_rows = []
 
# iterating over each row in the input list
for 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 result
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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads