Skip to content

Instantly share code, notes, and snippets.

@cmower
Last active October 12, 2023 07:13
Show Gist options
  • Save cmower/6b280456be76788143a4af8b6bc9f472 to your computer and use it in GitHub Desktop.
Save cmower/6b280456be76788143a4af8b6bc9f472 to your computer and use it in GitHub Desktop.
Print layout of a python dict/list, i.e. only keys/indexes and the types of the values.
def print_type_layout(x, indent=0, incr=2):
if isinstance(x, dict):
print(" " * indent, "{")
for k, v in x.items():
print(" " * indent, f"'{k}':", end="")
if isinstance(v, (dict, list)):
print("")
print_type_layout(v, indent=indent + incr)
else:
print(f" {type(v)}")
print(" " * indent, "}")
elif isinstance(x, list):
print(" " * indent, "[")
for i, v in enumerate(x):
print(" " * indent, f"{i}:", end="")
if isinstance(v, (dict, list)):
print("")
print_type_layout(v, indent=indent + incr)
else:
print(f" {type(v)}")
print(" " * indent, "]")
else:
print(type(x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment