Skip to content

Instantly share code, notes, and snippets.

@ryanuber
Created July 26, 2013 23:44
Show Gist options
  • Save ryanuber/6092975 to your computer and use it in GitHub Desktop.
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.
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