Created
July 26, 2013 23:44
-
-
Save ryanuber/6092975 to your computer and use it in GitHub Desktop.
Determine depth of data structure in Python. This recursive function will return the depth as an integer of the deepest-nested object.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def depth(data): | |
d = 1 | |
if type(data) is dict: | |
for item in data.keys(): | |
if type(data[item]) is dict or type(data[item]) is list: | |
d += depth(data[item]) | |
elif type(data) is list: | |
for item in data: | |
if type(item) is dict or type(item) is list: | |
d += depth(item) | |
return d |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment