For example, if we have a dict like {a: {b: c}, d: e}
, then the max level for this dict is 2.
I've been thinking about the method to find the max level of an arbitrary dict for 2 days, but didn't figure out a solution.
How to do that?
For example, if we have a dict like {a: {b: c}, d: e}
, then the max level for this dict is 2.
I've been thinking about the method to find the max level of an arbitrary dict for 2 days, but didn't figure out a solution.
How to do that?
Using recursion:
def nested_depth(d):
if not isinstance(d, dict):
return 0
if not d:
return 1
return 1 + max(nested_depth(v) for v in d.values())