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. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course