How to find the number of nested lists in a list?
You can use a recursive function as following:
In [14]: def count_lists(l):
...: return sum(1 + count_lists(i) for i in l if isinstance(i,list))
...:
In [15]:
In [15]: x=[1,2,[[[]]],[[]],3,4,[1,2,3,4,[[]] ] ]
In [16]: count_lists(x)
Out[16]: 8
This seems to do the job:
def count_list(l):
count = 0
for e in l:
if isinstance(e, list):
count = count + 1 + count_list(e)
return count