python-autoviv: Autovivification for Python Dictionaries

1.0.4 · active · verified Fri Apr 17

python-autoviv provides autovivification for Python dictionaries, allowing the creation of deeply nested data structures on-the-fly simply by accessing keys. It mimics Perl's default behavior, eliminating the need for explicit intermediate dictionary initializations. The current version is 1.0.4, with releases focusing on stability and minor enhancements.

Common errors

Warnings

Install

Imports

Quickstart

Initialize an `AutoVivification` object and create nested dictionary structures by simply assigning values to deeply nested keys. Intermediate dictionaries are automatically created.

from autoviv import AutoVivification

d = AutoVivification()
d[1]['one'][2]['two'] = "Nested is as nested does."
d[1]['one'][2]['three'] = "Or so they say."

print(d)
# Expected output: {1: {'one': {2: {'two': 'Nested is as nested does.', 'three': 'Or so they say.'}}}}

# Accessing a non-existent path creates it
if 'new_key' not in d[1]['one']:
    print("'new_key' does not exist in d[1]['one'] initially.")

d[1]['one']['new_key']['sub_key'] = 42
print(f"Value at d[1]['one']['new_key']['sub_key']: {d[1]['one']['new_key']['sub_key']}")
# Expected output: Value at d[1]['one']['new_key']['sub_key']: 42

view raw JSON →