A flexible utility for flattening dict-like objects in Python.
Project description
Please visit the GitHub repository for more information.
flatten-dict
A flexible utility for flattening dict-like objects in Python.
Introduction
This Python package provide a function flatten() for flattening dict-like objects. It also provides some key joining methods (reducer), and you can choose the reducer you want or even implement your own reducer. You can also choose to invert the resulting flat dict.
Documentation
def flatten(d, reducer='tuple', inverse=False):
"""Flatten dict-like object.
Parameters
----------
d: dict-like object
The dict that will be flattened.
reducer: {'tuple', 'path', function} (default: 'tuple')
The key joining method. If a function is given, the function will be
used to reduce.
'tuple': The resulting key will be tuple of the original keys
'path': Use ``os.path.join`` to join keys.
inverse: bool (default: False)
Whether you want invert the resulting key and value.
Returns
-------
flat_dict: dict
"""
Examples
In [1]: from flatten_dict import flatten
In [2]: normal_dict = {
...: 'a': '0',
...: 'b': {
...: 'a': '1.0',
...: 'b': '1.1',
...: },
...: 'c': {
...: 'a': '2.0',
...: 'b': {
...: 'a': '2.1.0',
...: 'b': '2.1.1',
...: },
...: },
...: }
In [3]: flatten(normal_dict)
Out[3]:
{('a',): '0',
('b', 'a'): '1.0',
('b', 'b'): '1.1',
('c', 'a'): '2.0',
('c', 'b', 'a'): '2.1.0',
('c', 'b', 'b'): '2.1.1'}
In [4]: flatten(normal_dict, reducer='path')
Out[4]:
{'a': '0',
'b/a': '1.0',
'b/b': '1.1',
'c/a': '2.0',
'c/b/a': '2.1.0',
'c/b/b': '2.1.1'}
In [5]: flatten(normal_dict, reducer='path', inverse=True)
Out[5]:
{'0': 'a',
'1.0': 'b/a',
'1.1': 'b/b',
'2.0': 'c/a',
'2.1.0': 'c/b/a',
'2.1.1': 'c/b/b'}
In [6]: def underscore_reducer(k1, k2):
...: if k1 is None:
...: return k2
...: else:
...: return k1 + "_" + k2
...:
In [7]: flatten(normal_dict, reducer=underscore_reducer)
Out[7]:
{'a': '0',
'b_a': '1.0',
'b_b': '1.1',
'c_a': '2.0',
'c_b_a': '2.1.0',
'c_b_b': '2.1.1'}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
flatten-dict-0.0.3.tar.gz
(4.0 kB
view details)
Built Distribution
File details
Details for the file flatten-dict-0.0.3.tar.gz
.
File metadata
- Download URL: flatten-dict-0.0.3.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 35fa7164b1947cf5d6450dd825e7ba87ccd202251dc2429dce831a81b742e720 |
|
MD5 | b846833ec05bbea2357f0bb39c0b4311 |
|
BLAKE2b-256 | 9a5abc3a3331e31f1fb95f247a1204f0ba2741db1737afeea0e4e01c8f871aff |
File details
Details for the file flatten_dict-0.0.3-py2.py3-none-any.whl
.
File metadata
- Download URL: flatten_dict-0.0.3-py2.py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 62e14d3526a2756af5b1845d683d08a6556dec5664dbf5ed6fb20566d697dfe8 |
|
MD5 | 83f983412c415e366d6fada37f8bb54b |
|
BLAKE2b-256 | 7af37311f421e9d9906aa8b2c43d3d3869abf1269ee3b7a98864051d42014da5 |