Given a list of lists, write a Python program to count the number of lists contained within the list of lists.
Input : [[1, 2, 3], [4, 5], [6, 7, 8, 9]] Output : 3 Input : [, ['Bob'], ['Delhi'], ['x', 'y']] Output : 4
Method #1 : Using len()
Method #2 : Using type()
Use a for loop and in every iteration to check if the type of the current item is a list or not, and accordingly increment ‘count’ variable. This method has a benefit over approach #1, as it works well for a list of heterogeneous elements.
A one-liner alternative approach for the above code is given below:
Method #3 : Using isinstance() method
- Python program to apply itertools.product to elements of a list of lists
- Python | Merge List with common elements in a List of Lists
- Python | Sorting list of lists with similar list elements
- Python | Zipping two lists of lists
- Python - Sum of different length Lists of list
- Python | Split list into lists by particular value
- Python | Maximum sum of elements of list in a list of lists
- Python | Convert List of lists to list of Strings
- Python | Ways to sum list of lists and return sum list
- Python | Check if a list exists in given list of lists
- Python | Product of kth column in List of Lists
- Python | Merge two list of lists according to first element
- Python | Get positive elements from given list of lists
- Python | Custom Multiplication in list of lists
- Python | Convert a list into tuple of lists
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.