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 developement
cd trace-dkey
python -m venv .venv
source .venv/bin/activate
Example
>>> 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
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 is addressing a wide range of question asked on stackoverflow about key inside a nested dict
Atleast 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
Hashes for trace_dkey-0.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d09d275334b4a88ec5d073b54625ed9f1777a8235ea47d0f706f7723d556aaf3 |
|
MD5 | 0e9950aaa66bbf878406045215cebb63 |
|
BLAKE2b-256 | 65d797639c07b37551ac711830bc9c4a111a48ce1e4e2a77c88b35854a48ef59 |