Python Program to count the number of lists in a list of lists

Python provides several approaches to count the number of lists contained within a list of lists. This is useful when working with nested data structures where you need to determine how many sublists exist. In this article, we'll explore three different methods to count lists in a list of lists using Python.

Using Iterative Approach

The iterative approach uses a simple loop to count lists by checking each element's type ?

Algorithm

  • Step 1 Create a function that takes the list of lists as a parameter.

  • Step 2 Initialize a counter variable to track the number of lists found.

  • Step 3 Iterate through each element using a for loop.

  • Step 4 Use isinstance() to check if the current element is a list.

  • Step 5 If the element is a list, increment the counter by one.

  • Step 6 Return the final count.

Example

def count_lists_iterative(list_of_lists):
    count = 0
    for element in list_of_lists:
        if isinstance(element, list):
            count += 1
    return count

# Test data with mixed elements
data = [[1, 2, 3], [4, 5], [6, 7, 8], 'Hello', [9, 10]]
result = count_lists_iterative(data)
print("The number of lists in the list of lists is:", result)
The number of lists in the list of lists is: 4

Using Recursive Approach

The recursive approach handles nested lists of arbitrary depth by making recursive calls for each sublist ?

Algorithm

  • Step 1 Create a recursive function that takes the list of lists as a parameter.

  • Step 2 Initialize a counter variable to track lists found.

  • Step 3 Iterate through each element using a for loop.

  • Step 4 Use isinstance() to check if the current element is a list.

  • Step 5 If the element is a list, increment the counter and recursively call the function.

  • Step 6 Return the total count including nested lists.

Example

def count_lists_recursive(list_of_lists):
    count = 0
    for element in list_of_lists:
        if isinstance(element, list):
            count += 1
            count += count_lists_recursive(element)
    return count

# Test with nested lists
data = [[1, 2, 3], [4, [5, 6]], [7, 8, 9], 'Hello', [10]]
result = count_lists_recursive(data)
print("The number of lists (including nested) is:", result)
The number of lists (including nested) is: 5

Using List Comprehension

List comprehension provides a concise and elegant way to count lists in a single line ?

Algorithm

  • Step 1 Create a function that takes the list of lists as a parameter.

  • Step 2 Use list comprehension to filter elements that are lists.

  • Step 3 Return the length of the filtered list.

Example

def count_lists_comprehension(list_of_lists):
    return len([element for element in list_of_lists if isinstance(element, list)])

# Test data
data = [[1, 2, 3], [4, 5], [6, 7, 8], 'Hello', [9, 10]]
result = count_lists_comprehension(data)
print("The number of lists in the list of lists is:", result)
The number of lists in the list of lists is: 4

Comparison

Method Handles Nested Lists Code Length Best For
Iterative No Medium Simple, readable solution
Recursive Yes Medium Deeply nested structures
List Comprehension No Short Concise, Pythonic code

Conclusion

Use the iterative approach for simple counting, the recursive approach for nested structures, and list comprehension for concise code. Choose the method that best fits your data structure and requirements.

Updated on: 2026-03-27T13:42:59+05:30

486 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements