Last active
November 22, 2024 16:34
-
-
Save ionymikler/18cc7fdd9b244100061daf81b9313f91 to your computer and use it in GitHub Desktop.
better (recursive) dictionary print
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
def print_dict(dictionary, ident='', braces=1): | |
""" Recursively prints nested dictionaries.""" | |
cached_dicts = [] | |
for key, value in dictionary.items(): | |
if isinstance(value, dict): | |
cached_dicts.append((key, value)) | |
else: | |
print(f'{ident}{key} = {value}') | |
for key, value in cached_dicts: | |
print(f'{ident}{braces*"["}{key}{braces*"]"}') | |
print_dict(value, ident + ' ', braces + 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment