{"id":10155,"library":"python-autoviv","title":"python-autoviv: Autovivification for Python Dictionaries","description":"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.","status":"active","version":"1.0.4","language":"en","source_language":"en","source_url":"https://github.com/derek-miller/python-autoviv","tags":["autovivification","dictionary","nested-data","data-structure","perl-like"],"install":[{"cmd":"pip install python-autoviv","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"The PyPI package name is `python-autoviv`, but the actual module name to import is `autoviv`.","wrong":"from python_autoviv import AutoVivification","symbol":"AutoVivification","correct":"from autoviv import AutoVivification"}],"quickstart":{"code":"from autoviv import AutoVivification\n\nd = AutoVivification()\nd[1]['one'][2]['two'] = \"Nested is as nested does.\"\nd[1]['one'][2]['three'] = \"Or so they say.\"\n\nprint(d)\n# Expected output: {1: {'one': {2: {'two': 'Nested is as nested does.', 'three': 'Or so they say.'}}}}\n\n# Accessing a non-existent path creates it\nif 'new_key' not in d[1]['one']:\n    print(\"'new_key' does not exist in d[1]['one'] initially.\")\n\nd[1]['one']['new_key']['sub_key'] = 42\nprint(f\"Value at d[1]['one']['new_key']['sub_key']: {d[1]['one']['new_key']['sub_key']}\")\n# Expected output: Value at d[1]['one']['new_key']['sub_key']: 42","lang":"python","description":"Initialize an `AutoVivification` object and create nested dictionary structures by simply assigning values to deeply nested keys. Intermediate dictionaries are automatically created."},"warnings":[{"fix":"Always double-check key names, especially when assigning. Consider using `dict.get()` with a default value or `if 'key' in d:` checks if you need to strictly verify key existence before modification.","message":"Autovivification can mask typos. If you misspell a key (e.g., `d['usrname']` instead of `d['username']`), it will silently create a new key instead of raising a `KeyError`, potentially leading to hard-to-debug data inconsistencies.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Be mindful of performance for extremely large or sparse datasets. If memory is a critical concern, consider alternative strategies or ensure all vivified paths are eventually populated with meaningful data.","message":"Every intermediate key access implicitly creates a new `AutoVivification` instance if the path doesn't exist. For very sparse or deeply nested data structures, this could consume more memory than a carefully constructed regular dictionary, as many empty intermediate objects might be instantiated.","severity":"gotcha","affected_versions":"All versions"},{"fix":"To recursively convert the entire structure to standard Python dictionaries, you'll need a helper function that iterates through the `AutoVivification` instance and converts all nested instances as well.","message":"Converting an `AutoVivification` instance to a standard Python `dict` using `dict(autoviv_instance)` will only convert the top level. Nested `AutoVivification` objects within the structure will remain `AutoVivification` instances, not standard `dict`s.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Change your import statement from `from python_autoviv import AutoVivification` to `from autoviv import AutoVivification`.","cause":"The PyPI package is `python-autoviv`, but the actual Python module name to import from is `autoviv`.","error":"ModuleNotFoundError: No module named 'python_autoviv'"},{"fix":"Understand that `AutoVivification` is designed to create keys on access. If you need standard `KeyError` behavior or want to explicitly check for key existence, use `if 'key' in my_autoviv_dict:` or `my_autoviv_dict.get('key', default_value)`.","cause":"This is the core behavior of autovivification. Any access to a non-existent key path (e.g., `d['new_key']`) implicitly creates a new `AutoVivification` object at that key, rather than raising a `KeyError`.","error":"Dictionary key created unexpectedly without explicit assignment or `KeyError`"}]}