Python | Program to count number of lists in a list of lists
Given a list of lists, write a Python program to count the number of lists contained within the list of lists.
Examples:
Input : [[1, 2, 3], [4, 5], [6, 7, 8, 9]] Output : 3
Input : [[1], ['Bob'], ['Delhi'], ['x', 'y']] Output : 4
Method #1 : Using len()
Python3
# Python3 program to Count number # of lists in a list of lists def countList(lst): return len (lst) # Driver code lst = [[ 1 , 2 , 3 ], [ 4 , 5 ], [ 6 , 7 , 8 , 9 ]] print (countList(lst)) |
3
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.
Python3
# Python3 program to Count number # of lists in a list of lists def countList(lst): count = 0 for el in lst: if type (el) = = type ([]): count + = 1 return count # Driver code lst = [[ 1 , 2 , 3 ], [ 4 , 5 ], [ 6 , 7 , 8 , 9 ]] print (countList(lst)) |
3
A one-liner alternative approach for the above code is given below:
Python3
def countList(lst): return sum ( type (el) = = type ([]) for el in lst) |
Method #3 : Using isinstance() method
Python3
# Python3 program to Count number # of lists in a list of lists def countList(lst): return sum ( isinstance (i, list ) for i in lst) # Driver code lst = [[ 1 , 2 , 3 ], [ 4 , 5 ], [ 6 , 7 , 8 , 9 ]] print (countList(lst)) |
3
Method#4: Using the list comprehension
Python3
lst = [[ 1 , 2 , 3 ], [ 4 , 5 ], [ 6 , 7 , 8 , 9 ]] x = [i for i in lst] print ( len (x)) |
3
Method #5: Using enumerate function
Python3
lst = [ "[1, 2, 3]" , "[4, 5]" , "[6, 7, 8, 9]" ] x = [ list (i) for i in enumerate (lst)] print ( len (x)) |
3
Method #6: Using lambda function
Python3
lst = [[ 1 , 2 , 3 ], [ 4 , 5 ], [ 6 , 7 , 8 , 9 ]] x = list ( filter ( lambda i: (i),lst)) print ( len (x)) |
3
Method #7: Using map()
Python3
lst = [[ 1 , 2 , 3 ], [ 4 , 5 ], [ 6 , 7 , 8 , 9 ]] x = list ( map ( str ,lst) ) print ( len (x)) |
3
Method #8: Using eval()
Python3
lst = [ "[1, 2, 3]" , "[4, 5]" , "[6, 7, 8, 9]" ] x = list ( map ( eval ,lst) ) print ( len (x)) |
3
Method #9 : Using recursion
This approach involves reducing the length of the list by 1 at each recursive step and increasing the count by 1 if the first element of the list is a list. The function returns 0 when the list is empty.
Python3
def count_lists(lst): if lst = = []: return 0 return 1 + count_lists(lst[ 1 :]) lst = [[ 1 , 2 , 3 ], [ 4 , 5 ], [ 6 , 7 , 8 , 9 ]] print (count_lists(lst)) #This code is contributed by Edula Vinay Kumar Reddy |
3
The time complexity of the recursive approach to count the number of lists in a list of lists is O(n), where n is the total number of elements in the list of lists. This is because the function processes each element of the list exactly once.
The space complexity of the recursive approach is O(n) as well, since the maximum depth of the recursion tree is n in the worst case. For example, if the list of lists is a list of n single-element lists, the recursion tree will have a maximum depth of n. At each level of the tree, a new frame is added to the call stack, which takes up space in memory.
Please Login to comment...