Skip to content

Instantly share code, notes, and snippets.

@ionymikler
Last active November 22, 2024 16:34
better (recursive) dictionary print
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