Skip to main content

Please visit the GitHub repository for more information.

flatten-dict

https://img.shields.io/travis/ianlini/flatten-dict/master.svg https://img.shields.io/pypi/v/flatten-dict.svg https://img.shields.io/pypi/l/flatten-dict.svg

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'}

Release files for flatten-dict 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flatten-dict 0.0.1
File Size Uploaded
flatten-dict-0.0.1.tar.gz 2.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flatten-dict 0.0.1
File Interpreter ABI Platform
flatten_dict-0.0.1-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 6.3 kB

Release files / flatten-dict-0.0.1.tar.gz

Download URL flatten-dict-0.0.1.tar.gz
Size 2.3 kB
Tags Source
SHA-256 checksum
How to use checksums
a02e3e643169933756c925339e2e93d781bd09fa0347e5b980fc759319fb9364
BLAKE2b-256 checksum
How to use checksums
e6aa4a5762c82d98cbe7019973ca5c203a521c8533fb364597b9e78b47b031c9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / flatten_dict-0.0.1-py2.py3-none-any.whl

Download URL flatten_dict-0.0.1-py2.py3-none-any.whl
Size 4.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
72201d09e27bfa6b95b6216ab0024ae6f9c3e6c81346920f43eb9f3811a68113
BLAKE2b-256 checksum
How to use checksums
9650981a4011b34dbe60736859686416e2feabbfe82cfad58b839b9f2ffb8f55
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

0.5.0

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

0.0.3

2 release files

0.0.2

2 release files

This release

0.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page