Created
September 29, 2017 09:15
-
-
Save danie1k/47a9d056ff03e59cdea100ca88288bc1 to your computer and use it in GitHub Desktop.
Total recursive dict len counter
This file contains hidden or 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
# 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