https://github.com/erdogant/dicter
Python package with advanced dictionary functions. Traverse through nested dicts. Set and get multiple keys. Flattens dicts. Store and load in json and more!
https://github.com/erdogant/dicter
compare compare-data dictionary flatten json load nested save traversal
Last synced: 5 months ago
JSON representation
Python package with advanced dictionary functions. Traverse through nested dicts. Set and get multiple keys. Flattens dicts. Store and load in json and more!
- Host: GitHub
- URL: https://github.com/erdogant/dicter
- Owner: erdogant
- License: mit
- Created: 2023-01-01T11:15:46.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2025-04-24T19:33:40.000Z (9 months ago)
- Last Synced: 2025-06-25T09:04:26.066Z (7 months ago)
- Topics: compare, compare-data, dictionary, flatten, json, load, nested, save, traversal
- Language: Python
- Homepage:
- Size: 6.67 MB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.rst
- Funding: .github/FUNDING.yml
- License: LICENSE
- Citation: CITATION.cff
Awesome Lists containing this project
README
[](https://img.shields.io/pypi/pyversions/dicter)
[](https://pypi.org/project/dicter/)
[](https://erdogant.github.io/dicter/)
[](https://github.com/erdogant/dicter/)
[](https://pepy.tech/project/dicter)
[](https://pepy.tech/project/dicter)
[](https://github.com/erdogant/dicter/blob/master/LICENSE)
[](https://github.com/erdogant/dicter/network)
[](https://github.com/erdogant/dicter/issues)
[](http://www.repostatus.org/#active)
[](https://zenodo.org/badge/latestdoi/584101058)


[](https://erdogant.github.io/dicter/pages/html/Documentation.html#)
* ``dicter`` is Python package with advanced dictionary functions:
* Traverse through nested dicts to retrieve key-path.
* Set value in dictionary using key-path
* Get value in dictionary using key-path.
* Flattens dicts.
* Compare two dicts.
* Store and load in json.
#
**Star this repo if you like it! ⭐️**
#
## Documentation
* [**dicter documentation pages (Sphinx)**](https://erdogant.github.io/dicter/)
## Installation
* Install dicter from PyPI (recommended). dicter is compatible with Python 3.6+ and runs on Linux, MacOS X and Windows.
* A new environment can be created as following:
```bash
pip install -U dicter
```
* Alternatively, you can install from the GitHub source:
```bash
# Directly install from github source
pip install git+https://github.com/erdogant/dicter
```
## Examples
#### Import dicter package
```python
import dicter as dt
```
#### Traverse all paths in dictionary.
```python
import dicter as dt
# Example dict:
d = {'level_a': 1, 'level_b': {'a': 'hello world'}, 'level_c': 3, 'level_d': {'a': 1, 'b': 2, 'c': {'e': 10}}, 'level_e': 2}
# Walk through dict to get all paths
paths = dt.traverse(d)
print(paths)
# [[['level_a'], 1],
# [['level_c'], 3],
# [['level_e'], 2],
# [['level_b', 'a'], 'hello world'],
# [['level_d', 'a'], 1],
# [['level_d', 'b'], 2],
# [['level_d', 'c', 'e'], 10]]
```
#### Get value from dictionary using nested keys.
```python
# Import dicter
import dicter as dt
# Example dictionary
d = {'level_a': 1, 'level_b': {'a': 'hello world'}, 'level_c': 3, 'level_d': {'a': 1, 'b': 2, 'c': {'e': 10}}, 'level_e': 2}
# Get the value for the nested path for:
value = dt.get_nested(d, key_path=["level_b", "a"])
print(value)
# 'hello world'
```
#### Set value from dictionary using nested keys.
```python
# Import dicter
import dicter as dt
# Example: New path and value in dictionary.
d = {}
key_path = ['person', 'address', 'city']
dt.set_nested(d, key_path, 'New York')
# Print updated dictionary
print(d)
# {'person': {'address': {'city': 'New York'}}}
```
#### Set value from dictionary using nested keys.
```python
# Import dicter
import dicter as dt
# Example dict
d = {'level_a': 1, 'level_b': {'a': 'hello world'}, 'level_c': 3, 'level_d': {'a': 1, 'b': 2, 'c': {'e': 10}}, 'level_e': 2}
# Flatten dictionary
dflat = dt.flatten(d)
print(d_flat)
# [['level_a', 1],
# ['a', 'hello world'],
# ['level_c', 3],
# ['a', 1],
# ['b', 2],
# ['e', 10],
# ['level_e', 2]]
```
#### Depth of dictionary.
```python
# Import dicter
import dicter as dt
d = {'level_a': 1, 'level_b': {'a': 'hello world'}, 'level_c': 3, 'level_d': {'a': 1, 'b': 2, 'c': {'e': 10}}, 'level_e': 2}
n = dt.depth(d)
```
#### Compare dictionary.
```python
# Import dicter
import dicter as dt
Example: Add
d1 = {'level_a': 1, 'level_b': {'a': 'hello world'}, 'level_c': 'new in d2'}
d2 = {'level_a': 1, 'level_b': {'a': 'hello world'}}
out = dt.compare(d1, d2)
print(out)
Example: Remove
d1 = {'level_a': 1, 'level_b': {'a': 'hello world'}}
d2 = {'level_a': 1, 'level_b': {'a': 'hello world'}, 'level_c': 'new in d2'}
out = dt.compare(d1, d2)
print(out)
Example: Modified
d1 = {'level_a': 1, 'level_b': {'a': 'hello world'}}
d2 = {'level_a': 1, 'level_b': {'a': 'modified'}}
out = dt.compare(d1, d2)
print(out['modified'])
```
#### Save and load dictionary.
```python
# Import dicter
import dicter as dt
d = {'level_a': None, 'level_b': {'a': 'hello world'}, 'level_c': True, 'level_d': 2.3, 'level_e': [[1,2,3], [1,2]]}
filepath='c:/temp/test/dicter_save.json'
# First save
dt.save(d, filepath=filepath, overwrite=True)
# Load
d = dt.load(filepath)
```
#
#### Citation
Please cite in your publications if this is useful for your research (see citation).
#### ☕ Support
If you find this project useful, consider supporting me: