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
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python | Maximum sum of elements of list in a list of lists
- Python | Ways to sum list of lists and return sum list
- Python | Check if a list exists in given list of lists
- Python | Convert list into list of lists
- Python | Sorting list of lists with similar list elements
- Python | Merge List with common elements in a List of Lists
- Python | Convert List of lists to list of Strings
- Python - Convert Key-Value list Dictionary to List of Lists
- Python - Convert List of Dictionaries to List of Lists
- Python | Zipping two lists of lists
- Python - Convert Lists into Similar key value lists
- Python program to apply itertools.product to elements of a list of lists
- Python program to select Random value form list of lists
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Convert list of string to list of list
- Python | Convert list of tuples to list of list
- Python | Convert List of String List to String List
- Creating a sorted merged list of two unsorted lists in Python
- Python | Get positive elements from given list of lists
- Python | Column deletion from list 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 firstname.lastname@example.org. 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.