Skip to content

Instantly share code, notes, and snippets.

@danie1k
Created September 29, 2017 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danie1k/47a9d056ff03e59cdea100ca88288bc1 to your computer and use it in GitHub Desktop.
Save danie1k/47a9d056ff03e59cdea100ca88288bc1 to your computer and use it in GitHub Desktop.
Total recursive dict len counter
# https://stackoverflow.com/a/35428134
def _counter(d):
# how many keys do we have?
yield len(d)
# stream the key counts of our children
for v in iter(d.values()):
if isinstance(v, dict):
for x in _counter(v):
yield x
def count_faster(d):
return sum(_counter(d))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment