Skip to main content

A tool to print out the structure of nested Python dict and list, like the `tree` command line tool.

Project description

nest-tree

A Python tool to visualize the structure of nested dictionaries and lists in a tree-like format, similar to the Unix tree command.

Installation

pip install nest-tree

Usage

Tl;dr

from nest_tree import tree

tree(data)

tree(data, leaf="none", level=None, print_output=True, return_output=False)

  • Parameters:
    • data (dict | list): The nested dictionary or list to visualize.
    • leaf (str): Controls leaf display - "none" (default), "value", or "type".
    • level (int | None): Maximum depth. If None, expand fully.
    • print_output (bool): If True, print to stdout. Default: True.
    • return_output (bool): If True, return what would be printed as a string. Default: False (in order to avoid busy output in interactive REPLs like IPython).
  • Return:
    • If return_output is True: the output is returned as a string. No return value otherwise.

Example

Consider the following nested list/dict

data = {
    "fruits": ["apple", "banana"],
    "vegetables": {
        "green": ["lettuce", "spinach"],
        "root": ["carrot", "potato"],
    },
    "requirement": [
        {"type": "protein", "amount": 100, "unit": "g"},
        {"type": "vitamin C", "amount": 80, "unit": "mg"}
    ]
}

tree(data, level=2)

├── fruits
│   ├── [0]
│   └── [1]
├── vegetables
│   ├── green
│   └── root
└── requirement
    ├── [0]
    └── [1]

tree(data, leaf="type")

├── fruits
│   ├── [0]
│   │   └── <class 'str'>
│   └── [1]
│       └── <class 'str'>
├── vegetables
│   ├── green
│   │   ├── [0]
│   │   │   └── <class 'str'>
│   │   └── [1]
│   │       └── <class 'str'>
│   └── root
│       ├── [0]
│       │   └── <class 'str'>
│       └── [1]
│           └── <class 'str'>
└── requirement
    ├── [0]
    │   ├── type
    │   │   └── <class 'str'>
    │   ├── amount
    │   │   └── <class 'int'>
    │   └── unit
    │       └── <class 'str'>
    └── [1]
        ├── type
        │   └── <class 'str'>
        ├── amount
        │   └── <class 'int'>
        └── unit
            └── <class 'str'>

tree(data, leaf="value")

├── fruits
│   ├── [0]
│   │   └── apple
│   └── [1]
│       └── banana
├── vegetables
│   ├── green
│   │   ├── [0]
│   │   │   └── lettuce
│   │   └── [1]
│   │       └── spinach
│   └── root
│       ├── [0]
│       │   └── carrot
│       └── [1]
│           └── potato
└── requirement
    ├── [0]
    │   ├── type
    │   │   └── protein
    │   ├── amount
    │   │   └── 100
    │   └── unit
    │       └── g
    └── [1]
        ├── type
        │   └── vitamin C
        ├── amount
        │   └── 80
        └── unit
            └── mg

Running Tests

python -m unittest tests.test_tree -v

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nest_tree-0.1.1.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nest_tree-0.1.1-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file nest_tree-0.1.1.tar.gz.

File metadata

  • Download URL: nest_tree-0.1.1.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for nest_tree-0.1.1.tar.gz
Algorithm Hash digest
SHA256 868df5a631a00be4a81ed3092efc3ff74a5f360effd0d92685ca5975649d5df3
MD5 4c42257a9227e5c8eb15473944f98048
BLAKE2b-256 95d5dd1f8326bb3de6452503942556d27e12fde1c04eca8ca82537613d43cce5

See more details on using hashes here.

File details

Details for the file nest_tree-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: nest_tree-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for nest_tree-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 442aeb326b4fa34b75c66821aa97a0a839dcc0d545408061c4010121ed4b2941
MD5 e56dbbf20d9deb7b0045bcdf6d21d05a
BLAKE2b-256 8bf0351f7942ef19e5f5df13200ec97ddc653c4c9af04711fe3cee7e895af714

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page