Python - Find the “depth” of an element in list in a recursive loop
问题 I want to know the depth of an element in a list in Python using a recursive loop (not a function) but it doesn't work. I find some answers with functions but it's not the point here. Something like 'a' is depth 2 and 'd' is depth 3 in the list below Here is my code: list1 = [['x','y'], ['z','p'], ['m',['a','b','c',['d','e']]]] level = 0 def print_list(l): for e in l: global level if type(e) == list: print_list(e) level +=1 else: print(str(e) + ",", str(level)) print_list(list1) Result: x, 0