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


Python gives an assortment of capable highlights and libraries for information control and investigation. When managing complex information structures, such as records of records, it can be advantageous to decide the number of records contained inside the list. In this article, we are going investigate three distinctive approaches to check the number of records in a list of records utilizing Python. We'll demonstrate the calculations, steps, and sentence structure, went with by code cases and yields, to assist you get it and apply these procedures viably.

Approach 1: By using Iterative Approach method

The primary approach includes utilizing an iterative calculation to check the number of records in a list of records. Here are the steps to execute this approach −

Algorithm

  • Step 1 − Creation of a function named count_lists_iterative(), that takes the list_of_lists as a contention.

  • Step 2 − Initialize a counter variable, number, to keep track of the number of records found.

  • Step 3 − Repeat through each element of the list_of_lists employing a for circle.

  • Step 4 − Utilize the isinstance() function to check on the off chance that the current component may be a list.

  • Step 5 − On the off chance that the component could be a list, increase the counter by one.

  • Step 6 − At last, return the number.

Example

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

t = [[1, 2, 3], [4, 5], [6, 7, 8], 'Hello', [9, 10]]
lst = count_lists_iterative(t)
print("The no. of lists in the list of lists is:", lst)

Output

The no. of lists in the list of lists is: 4

Approach 2: By using Recursive Approach

The second approach includes employing a recursive calculation to number the number of records in a list of records. This approach permits us to handle settled records of self-assertive profundity. Here are the steps to execute this approach −

Algorithm

  • Step 1 − Create a function count_lists_recursive(), that takes the list_of_lists as a argument.

  • Step 2 − Initialize a counter variable, to keep track of the number of records found.

  • Step 3 − Repeat through each element of the list_of_lists employing a for loop.

  • Step 4 − Utilize the isinstance() function to check if the current component may be a list.

  • Step 5 − In case the element could be a list, increase the counter by one.

  • Step 6 − If the element may be a list, call the count_lists_recursive() work recursively, passing the current element as the argument.

  • Step 7 − At long last, display the result.

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


t = [[1, 2, 3], [4, 5], [6, 7, 8], 'Hello', [9, 10]]
lst = count_lists_recursive(t)
print("The no. of lists in a list of lists is:", lst)

Output

The no. of lists in a list of lists is: 4

Approach 3: By List Comprehension Approach

The third approach includes utilizing list comprehension, a brief and exquisite way to build records in Python.

Algorithm

  • Step 1 − Specify the count_lists_list_comprehension(), that takes the list_of_lists as an contention.

  • Step 2 − Use list comprehension to form an unused list containing components from the list_of_lists that are themselves records.

  • Step 3 − Return the length of the unused list.

Example

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

lst = [[1, 2, 3], [4, 5], [6, 7, 8], 'Hello', [9, 10]]
n_lst = count_lists_list_comprehension(lst)
print("The no. of lists in a list of lists is:", n_lst)

Output

The no. of lists in a list of lists is: 4

Conclusion

In this article, we investigated three diverse approaches to number the number of records in a list of records utilizing Python. The iterative approach permits us to direct emphasize through the list and increase a counter variable when experiencing a sublist. The recursive approach handles settled records of self-assertive profundity by making recursive calls. Finally, the list comprehension approach offers a brief and exquisite arrangement by building an unused list with elements that fulfill the condition of being a list. Depending on the complexity of your information and your specific use case, you'll be able to select the foremost reasonable approach to effectively check records inside records in Python.

Updated on: 29-Aug-2023

94 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements