Python - Extract rows with Complex data types
Last Updated :
23 Jul, 2025
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))
OutputThe 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))
OutputThe 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))
OutputThe 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 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))
OutputThe 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.
Similar Reads
Python - Extract Particular data type rows 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
3 min read
Python program to extract rows from Matrix that has distinct data types Given a Matrix, the task is to write a Python program to extract rows with no repeated data types. Examples: Input : test_list = [[4, 3, 1], ["gfg", 3, {4:2}], [3, 1, "jkl"], [9, (2, 3)]]Output : [['gfg', 3, {4: 2}], [9, (2, 3)]]Explanation : [4, 3, 1] are all integers hence omitted. [9, (2, 3)] has
6 min read
Python program to read CSV without CSV module CSV (Comma Separated Values) is a simple file format used to store tabular data, such as a spreadsheet or database. CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the c
3 min read
Python program to read CSV without CSV module CSV (Comma Separated Values) is a simple file format used to store tabular data, such as a spreadsheet or database. CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the c
3 min read
Python program to read CSV without CSV module CSV (Comma Separated Values) is a simple file format used to store tabular data, such as a spreadsheet or database. CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the c
3 min read
Fastest Way to Read Excel File in Python Reading Excel files is a common task in data analysis and processing. Python provides several libraries to handle Excel files, each with its advantages in terms of speed and ease of use. This article explores the fastest methods to read Excel files in Python.Using pandaspandas is a powerful and flex
3 min read