{"id":1467,"library":"dpath","title":"dpath: Filesystem-like Pathing for Dictionaries","description":"dpath-python provides filesystem-like pathing and searching capabilities for nested dictionary and list structures. It allows you to get, set, delete, and search data within complex Python objects using a simple string path syntax. The current version is 2.2.0, and the library maintains an active release cadence with regular updates and bug fixes.","status":"active","version":"2.2.0","language":"python","source_language":"en","source_url":"https://github.com/dpath-maintainers/dpath-python","tags":["dictionary","path","json","data-manipulation","nested-data"],"install":[{"cmd":"pip install dpath","lang":"bash","label":"Install dpath"}],"dependencies":[],"imports":[{"note":"All primary functions are available directly under the 'dpath' module.","symbol":"dpath","correct":"import dpath"}],"quickstart":{"code":"import dpath\n\n# Sample data\ndata = {\n    \"user\": {\n        \"profile\": {\n            \"name\": \"Alice\",\n            \"age\": 30,\n            \"interests\": [\"coding\", \"photography\"]\n        },\n        \"settings\": {\n            \"theme\": \"dark\"\n        }\n    },\n    \"items\": [\n        {\"id\": 1, \"name\": \"itemA\"},\n        {\"id\": 2, \"name\": \"itemB\"}\n    ]\n}\n\nprint(\"Original data:\")\nprint(data)\nprint(\"-\" * 20)\n\n# Get a value\nname = dpath.get(data, '/user/profile/name')\nprint(f\"User name: {name}\")\n\n# Set a value (creates path if it doesn't exist)\ndpath.set(data, '/user/profile/age', 31)\nprint(f\"Updated age: {dpath.get(data, '/user/profile/age')}\")\ndpath.set(data, '/user/profile/city', 'New York')\nprint(f\"Added city: {dpath.get(data, '/user/profile/city')}\")\n\n# Get all values matching a pattern (returns a generator)\nprint(\"\\nAll item names:\")\nfor item_name in dpath.values(data, '/items/*/name'):\n    print(f\"- {item_name}\")\n\n# Merge data (default is to update/replace)\nnew_settings = {\"user\": {\"settings\": {\"notifications\": True}}}\ndpath.merge(data, new_settings)\nprint(\"\\nData after merge:\")\nprint(data)\n\n# Delete a path\ndpath.delete(data, '/user/settings/notifications')\nprint(\"\\nData after deleting notifications:\")\nprint(data)\n","lang":"python","description":"This example demonstrates how to use `dpath.get`, `dpath.set`, `dpath.values`, `dpath.merge`, and `dpath.delete` to manipulate a nested dictionary and list structure. It shows getting a value, setting/creating new paths, iterating through matching values with wildcards, merging dictionaries, and deleting paths."},"warnings":[{"fix":"Ensure your project runs on Python 3.7 or newer. This version also introduced type hinting.","message":"dpath dropped support for Python 2.x. Any projects relying on older Python versions will need to upgrade to Python 3.7+.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"If you intend to create lists, explicitly initialize them (e.g., `data['path'] = []`) before setting values with integer indices, or ensure your paths are unambiguous for list creation.","message":"Behavior for interpreting integer-like path segments when creating new paths was changed to resolve ambiguity. This may lead to dictionary keys being created (e.g., `{'0': ...}`) instead of list indices (e.g., `[..., ...]`) if the target path is not an existing list or explicitly initialized as one.","severity":"breaking","affected_versions":">=2.1.4"},{"fix":"To get a list, consume the generator: `list(dpath.values(data, '*/name'))` or iterate directly: `for val in dpath.values(data, '*/name'): ...`.","message":"`dpath.values()` and `dpath.search()` return generators, not immediate lists. If you expect a list, you must explicitly convert the generator.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Understand the `merge_types` parameter (e.g., `dpath.merge(data, src, merge_types=dpath.MERGE_ADDITIVE)`) to control how lists and dicts are combined, particularly to extend lists instead of replacing them.","message":"The `dpath.merge()` function's default behavior can replace entire lists or dictionaries. For more granular control over merging, especially for lists, the `merge_types` parameter should be used.","severity":"gotcha","affected_versions":"All versions (clarified with Enum in >=2.1.0)"},{"fix":"To prevent `KeyError` for non-existent paths, explicitly provide a `default` value to `dpath.get()` (e.g., `dpath.get(data, 'path', default=None)`) or wrap the call in a `try-except KeyError` block.","message":"`dpath.get()` raises `KeyError` when a path does not exist, by default. Users expecting behavior similar to Python's `dict.get()` (which returns `None` for missing keys) might encounter unexpected `KeyError`s.","severity":"gotcha","affected_versions":"All versions"},{"fix":"To prevent `KeyError`, ensure the path exists before calling `dpath.get()`, or wrap the call in a `try-except KeyError` block. For checking existence, consider `dpath.search(data, path, yielded=True, limit=1)` which will yield an empty generator if the path is not found, or directly check `path in data` if using a simple path string.","message":"`dpath.get()` raises a `KeyError` if the specified path does not exist. Unlike `dict.get()`, it does not accept a default value to return for missing paths.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"search_vec":"'2.2.0':49 'activ':55 'allow':26 'bug':62 'cadenc':57 'capabl':18 'complex':36 'current':46 'data':34,68,72 'data-manipul':67 'delet':31 'dictionari':7,21,64 'dpath':1,9 'dpath-python':8 'filesystem':3,13 'filesystem-lik':2,12 'fix':63 'get':29 'json':66 'librari':52 'like':4,14 'list':23 'maintain':53 'manipul':69 'nest':20,71 'nested-data':70 'object':38 'path':5,15,43,65 'provid':11 'python':10,37 'regular':59 'releas':56 'search':17,33 'set':30 'simpl':41 'string':42 'structur':24 'syntax':44 'updat':60 'use':39 'version':47 'within':35","created_at":"2026-04-09T03:49:12.028721+00:00","updated_at":"2026-04-16T14:41:23.036969+00:00","problems":[{"fix":"Use `dpath.get(obj, path, default=None)` to provide a default value if the path is not found, or verify the path's existence using `dpath.search()` before attempting to retrieve its value.","cause":"The specified path or a segment of the path (glob) does not exist within the dictionary structure, causing dpath to fail when trying to access a non-existent key.","error":"KeyError: 'path/to/missing/key'"},{"fix":"Import functions directly from the `dpath` package instead of `dpath.util`. For example, change `import dpath.util` to `import dpath` and use `dpath.get()` instead of `dpath.util.get()`.","cause":"The `dpath.util` package has been deprecated, and its functions have been moved directly to the top-level `dpath` package.","error":"ModuleNotFoundError: No module named 'dpath.util'"},{"fix":"Ensure that the parent nodes in the path you are trying to create or modify are mutable collection types (dictionaries or lists) that can hold further nested structures. Initialize intermediate paths with appropriate dictionary or list types if they don't exist, or use `dpath.new()` with `creator=dict` or `creator=list` for specific nodes.","cause":"This error occurs when `dpath.new()` or `dpath.set()` attempts to create or modify nested path entries inside an object that is not a dictionary or a list (e.g., an integer or a string), which cannot be subscripted.","error":"TypeError: 'int' object is not subscriptable"},{"fix":"Refine the glob pattern to be more specific to ensure it matches only one element. If multiple matches are intended, use `dpath.search()` or `dpath.values()` which are designed to return multiple results.","cause":"`dpath.get()` is designed to retrieve a single, unambiguous value. This error is raised when the provided glob pattern matches multiple elements in the dictionary, preventing a unique result.","error":"ValueError: More than one leaf matched the glob"}],"ecosystem":"pypi","meta_description":null,"install_score":100,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"2.2.0","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/dpath-maintainers/dpath-python","docs":null,"changelog":null,"pypi":"https://pypi.org/project/dpath/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["data"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-27","next_check":"2026-07-28","install_tag":"verified"}}