Open In App

Python | Test for nested list

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Sometimes, while working with Python lists, we might have a problem in which we need to find that a list is a Matrix or a list contains a list as its element. This problem can come in the Data Science domain as it involves the use of Matrices more than often. Let’s discuss the certain way in which this task can be performed.

Test for Nested List in Python

Below are the ways by which we can test for nested lists:

  • Using any() and instance()
  • Using type() Method
  • Using recursive function

Test for Nested List Using any() and instance()

The combination of the above functions can be used to perform this task. The any() is used to check for each of the occurrences and the isinstance() is used to check for the list. 

Python3




# initialize list
test_list = [[5, 6], 6, [7], 8, 10]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Test for nested list
# using any() + isinstance()
res = any(isinstance(sub, list) for sub in test_list)
 
# printing result
print("Does list contain nested list ? : " + str(res))


Output

The original list is : [[5, 6], 6, [7], 8, 10]
Does list contain nested list ? : True



Time complexity: O(n), where n is the number of elements in the list.
Auxiliary space: O(1)

Python Test Nested List Using type() method

In this example, the Python code determines if the list test_list contains any nested lists by iterating through its elements and setting the variable res to True if a nested list is found. The final output indicates whether the original list contains a nested list or not.

Python3




# initialize list
test_list = [[5, 6], 6, [7], 8, 10]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Test for nested list
res=False
for i in test_list:
    if type(i) is list:
        res=True
        break
# printing result
print("Does list contain nested list ? : " + str(res))


Output

The original list is : [[5, 6], 6, [7], 8, 10]
Does list contain nested list ? : True



Time Complexity: O(n)
Auxiliary Space: O(1)

Testing for Nested List Using Recursive Function

In this example, the Python function has_nested_list employs recursion to determine whether a given list lst contains any nested lists, including nested tuples and sets. The example usage demonstrates the detection of a nested list within the provided list [[5, 6], 6, [7], 8, 10], resulting in the output True.

Python3




def has_nested_list(lst):
    for elem in lst:
        if isinstance(elem, list):
            return True
        elif isinstance(elem, (tuple, set)):
            # check nested tuples and sets too
            if has_nested_list(list(elem)):
                return True
    return False
 
# Example usage
lst = [[5, 6], 6, [7], 8, 10]
result = has_nested_list(lst)
print(result)  # Output: True


Output

True



Time complexity: O(n)
Space complexity: O(m)
 



Last Updated : 05 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads