Python library to trace path of a particular key inside a nested dict
Project description
trace-dkey
Python library to trace path of a particular key inside a nested dict
Installation
- For stable version
pip install trace-dkey
- For development
cd trace-dkey
python -m venv .venv
source .venv/bin/activate
Example
you can use the library as python3 -m trace_dkey --file=test.json --key=name, test.json is
A JSON file containing name as key
>>> from trace_dkey import trace
>>> l={'a':{'b':{'c':{'d':{'e':{'f':1}}}}}}
>>> print(trace(l,'f'))
[['a', 'b', 'c', 'd', 'e', 'f']]
Now you can query it as l['a']['b']['c']['d']['e']['f']
>>> l['a']['b']['c']['d']['e']['f']
1
Documentation
For more details, you can refer to the documentation.
General Info
The value returned by the trace function is an array of paths, where each path is an array of dictionary keys.
Because of that, the library can be used in a practical way by taking advantage of this format.
In the example below we use the returned path to iterate over the dictionary keys and print the key value:
from trace_dkey import trace
l={'a':{'b':{'c':{'d':{'e':{'f':1}}}}}}
paths = trace(l,'f')
for path in paths:
dic = l
for key in path:
dic = dic[key]
print(dic)
This addresses a wide range of questions asked on StackOverflow about key inside a nested dict
At least 13 duplicate questions can be found on Stackoverflow
This can be tracked on https://you.com/search?q=find%20key%20in%20nested%20dictionary%20python
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
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
Built Distribution
File details
Details for the file trace_dkey-0.0.8.tar.gz
.
File metadata
- Download URL: trace_dkey-0.0.8.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c225a6ebd3da6a37651bdb812d6cc34830cec21cc8b6212da522cd7c28a44011 |
|
MD5 | 9bd1b99d825e869d696be245c3fb6c7e |
|
BLAKE2b-256 | c99b2771b68ecbdbd99d0b8c521dbba68b82a1bd185d0fba759edc8db7e19744 |
File details
Details for the file trace_dkey-0.0.8-py3-none-any.whl
.
File metadata
- Download URL: trace_dkey-0.0.8-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a9521d60b1b53ddc79bc10e01556527e3fe74d81ac10671ce0db70ef425cf6a5 |
|
MD5 | 77658aa1d077600c47e68eb30ad190ee |
|
BLAKE2b-256 | 0977ac509ef7ecfbde477dcec77b2018149de6bda3c96da1ca751725a80f249b |