Skip to main content

dictnavigator is a tool for working with nested dictionaries in Python. The DictNavigatorKey class allows easy retrieval, updating, and deletion of values by keys at any depth, even inside nested types (tuple, list, dictionary). It also provides a list of all keys at any nesting level. Especially useful for working with JSON data from APIs, simplifying access and modification of needed values.

Project description

dictnavigator

dictnavigator is a tool for working with nested dictionaries in Python. The main DictNavigatorKey class provides convenience methods for retrieving, updating, and deleting values for given keys at any dictionary depth, even if the key:value pair is contained in another nested data type (tuple, list, dictionary). In addition, it allows you to get a list of all keys in the dictionary at any nesting level. This library will come in handy if you work with data in JSON format, especially if you receive it from an API. It simplifies working with this data, allowing you to quickly retrieve the values you need and update them.

Requirements

Python >=3.5.2.

Install

pip install dictnavigator

Usage example

>>> from dictnavigator.navigator import DictNavigatorKey

>>> dictionary = {
        "key1": "value1",
        "key2": {
            "nested_key1": "nested_value1",
            "nested_key2": {
                "deeply_nested_key1": "deeply_nested_value1",
                "deeply_nested_key2": "deeply_nested_value2",
                "deeply_nested_key3": [{"list_key1": "list_deeply_value1"}],
                "deeply_nested_key4": ["deeply_nested_value4", "deeply_nested_value5"]
            }
        },
        "key3": [
            {"list_key1": "list_value1"},
            {"list_key2": "list_value2"},
            {"list_key3": "list_value3"}
        ],
        "key4": (
            {"tuple_key1": "tuple_value1"},
            {"tuple_key2": "tuple_value2"}
        )
    }

>>> navigator_one = DictNavigatorKey(dictionary)
>>> print(navigator_one.extract_keys_values("deeply_nested_key3"))
>>> print(navigator_one.extract_keys_values("list_key1"))
>>> print(navigator_one.extract_keys_values("key1"))
>>> print(navigator_one.extract_keys_values("tuple_key2"))

[{'deeply_nested_key3': [{'list_key1': 'list_deeply_value1'}]}]
[{'list_key1': 'list_deeply_value1'}, {'list_key1': 'list_value1'}]
[{'key1': 'value1'}]
[{'tuple_key2': 'tuple_value2'}]

>>> navigator_one.delete_key("key4", ({"tuple_key1": "tuple_value1"}, {"tuple_key2": "tuple_value2"}))
>>> navigator_one.update_value("deeply_nested_key4", ["deeply_nested_value4", "deeply_nested_value5"], ['Liza', 'Peter'])
>>> print(navigator_one.get_dict())

{'key1': 'value1', 'key2': {'nested_key1': 'nested_value1', 
                            'nested_key2': {'deeply_nested_key1': 'deeply_nested_value1', 'deeply_nested_key2': 'deeply_nested_value2', 
                                            'deeply_nested_key3': [{'list_key1': 'list_deeply_value1'}], 
                                            'deeply_nested_key4': ['Liza', 'Peter']}}, 
                    'key3': [{'list_key1': 'list_value1'}, {'list_key2': 'list_value2'}, {'list_key3': 'list_value3'}]}

>>> print(navigator_one.extract_keys_values("deeply_nested_key3"))
>>> print(navigator_one.extract_keys_values("deeply"))
>>> print(navigator_one.extract_all_keys())

[{'deeply_nested_key3': [{'list_key1': 'list_deeply_value1'}]}]
[]
['deeply_nested_key1', 'deeply_nested_key3', 'deeply_nested_key4', 'key1', 'list_key1', 'nested_key2', 'key3', 'deeply_nested_key2', 'key2', 'nested_key1', 'list_key2', 'list_key3']

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

dictnavigator-1.0.tar.gz (3.4 kB view details)

Uploaded Source

File details

Details for the file dictnavigator-1.0.tar.gz.

File metadata

  • Download URL: dictnavigator-1.0.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for dictnavigator-1.0.tar.gz
Algorithm Hash digest
SHA256 94a1b84993e5dfda9f96668e2cf08e5eb25ca55d7b929a2655de44230af260aa
MD5 aa72207503c2fe0d8f06c8b93afca407
BLAKE2b-256 3a44da3273735756bb7545331486510fb201bc93a5d42c00618d86bc00346c8a

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