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

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.
  • Otherwise, None.

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, leaf="none")

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

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.0.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.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: nest_tree-0.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 63e6968abdcc75ae8aa797d1db56c3389248477573986aed1e795649457fd817
MD5 3dff4373b40c3729bd69fb33be82c60e
BLAKE2b-256 82905331ead95fb3d318b6d46a440cef87840048a7770deccd7a247b67a02c0c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: nest_tree-0.1.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 19650765c7b40d3644a847323d1c7f8490d6505ba72a54a860c6d77c6ae4727f
MD5 a5ec1836496156bc063c65ae1dfaf1e2
BLAKE2b-256 9b6b0fb360c009c4fff7c70409d65ab2cd0bf09b2175a399f2891102e6742117

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