I'm trying to create a recursive algorithm to find the depth of a list, which is the largest number of nested lists
, but this is complicated, I can not find it. I realize the path that the algorithm has to make, but I still have not clearly understood how it has to do and how to implement it. If anyone could help me figure out how to do it better, it would be cool.
What I realized so far is that every time an element is a list, we have to increase a counter variable, but I'm not sure how to relate this variable to recursive calls.
I would like to know the process to get the solution and not a solution, because then you would better understand how recursion works ...
I've seen some recursive solutions in stackoverflow , for example this:
def flat(l):
depths = []
for item in l:
if isinstance(item, list):
depths.append(flat(item))
if len(depths) > 0:
return 1 + max(depths)
return 1
But even then it is not easy to understand why all calls and all steps ...