[PYTHON] Depth of nested list

Count the nested depths of the list. That's because it was fallen.

I just added a line to the one that was dropped to count the empty list.

def depth(l):
    if isinstance(l, list):
        if l == [] : return 1 # count a empty list
        return 1 + max(depth(item) for item in l)
    else:
        return 0

if __name__ == "__main__":
    l = ["1",[],"42",["2","13",["2","3",[]],["4"]],["2",[" "],"3"],"3"]
    print depth(l)

Recommended Posts

Depth of nested list
List of python modules
List of activation functions (2020)
Display of fractions (list)
Summary of Python3 list operations
Operation of filter (None, list)
List of self-made Docker images
Multidimensional array initialization of list
[Python] Copy of multidimensional list
List of useful coding styles
Judgment of if by list comprehension
List of ready-to-use word embed vectors
List of frequently used Linux commands
Generate a list of consecutive characters
About the basics list of Python basics
Note: List of customized special names
[Linux] List of Linux commands used in practice
Display a list of alphabets in Python 3
Get the column list & data list of CASTable
[python] Get a list of instance variables
String conversion of a list containing numbers
Basic grammar of Python3 series (list, tuple)
List of Atom packages I really use
Summary of built-in methods in Python list
[Machine learning] List of frequently used packages
Summary of how to use Python list
[Python] Get a list of folders only