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 and unflattening 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

Flatten

def flatten(d, reducer='tuple', inverse=False, enumerate_types=()):
    """Flatten `Mapping` object.

    Parameters
    ----------
    d : dict-like object
        The dict that will be flattened.
    reducer : {'tuple', 'path', Callable}
        The key joining method. If a `Callable` is given, the `Callable` 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
        Whether you want invert the resulting key and value.
    enumerate_types : Sequence[type]
        Flatten these types using `enumerate`.
        For example, if we set `enumerate_types` to ``(list,)``,
        `list` indices become keys: ``{'a': ['b', 'c']}`` -> ``{('a', 0): 'b', ('a', 1): 'c'}``.

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

If we have some iterable (e.g., list) in the dict, we will normally get this:

In [8]: flatten({'a': [1, 2, 3], 'b': 'c'})
Out[8]:
{('a',): [1, 2, 3],
 ('b',): 'c'}

If want to use its indices as keys, then we can use the parameter enumerate_types:

In [9]: flatten({'a': [1, 2, 3], 'b': 'c'}, enumerate_types=(list,))
Out[9]:
{('a', 0): 1,
 ('a', 1): 2,
 ('a', 2): 3,
 ('b',): 'c'}

We can even flatten a list directly:

In [10]: flatten([1, 2, 3], enumerate_types=(list,))
Out[10]:
{(0,): 1,
 (1,): 2,
 (2,): 3}

Unflatten

def unflatten(d, splitter='tuple', inverse=False):
    """Unflatten dict-like object.

    Parameters
    ----------
    d : dict-like object
        The dict that will be unflattened.
    splitter : {'tuple', 'path', Callable}
        The key splitting method. If a Callable is given, the Callable will be
        used to split.
        'tuple': Use each element in the tuple key as the key of the unflattened dict.
        'path': Use `pathlib.Path.parts` to split keys.
    inverse : bool
        Whether you want to invert the key and value before flattening.

    Returns
    -------
    unflattened_dict : dict
    """

Examples

In [1]: from flatten_dict import unflatten

In [2]: flat_dict = {
   ...:     ('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 [3]: unflatten(flat_dict)
Out[3]:
{'a': '0',
 'b': {'a': '1.0', 'b': '1.1'},
 'c': {'a': '2.0', 'b': {'a': '2.1.0', 'b': '2.1.1'}}}

In [4]: flat_dict = {
   ...:     '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]: unflatten(flat_dict, splitter='path')
Out[5]:
{'a': '0',
 'b': {'a': '1.0', 'b': '1.1'},
 'c': {'a': '2.0', 'b': {'a': '2.1.0', 'b': '2.1.1'}}}

In [6]: flat_dict = {
   ...:     '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 [7]: unflatten(flat_dict, splitter='path', inverse=True)
Out[7]:
{'a': '0',
 'b': {'a': '1.0', 'b': '1.1'},
 'c': {'a': '2.0', 'b': {'a': '2.1.0', 'b': '2.1.1'}}}

In [8]: def underscore_splitter(flat_key):
   ...:     return flat_key.split("_")
   ...:

In [9]: flat_dict = {
   ...:     '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 [10]: unflatten(flat_dict, splitter=underscore_splitter)
Out[10]:
{'a': '0',
 'b': {'a': '1.0', 'b': '1.1'},
 'c': {'a': '2.0', 'b': {'a': '2.1.0', 'b': '2.1.1'}}}

Metadata

Release files for flatten-dict 0.1.0

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.1.0
File Size Uploaded
flatten-dict-0.1.0.tar.gz 5.3 kB Details

Built distribution (wheel)

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

Total release size: 12.0 kB

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

Download URL flatten-dict-0.1.0.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
82944200b8c257d36778691045c53a3cf6925a069a1e8e0890b5e5f6da0e50de
BLAKE2b-256 checksum
How to use checksums
e9361795be8d8133be7a0da14c7cd7adfa6b8a8b8b584fc99e22a142d78fdb6b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.2

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

Download URL flatten_dict-0.1.0-py2.py3-none-any.whl
Size 6.8 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
f4f4901e93c253842afb400ddc5e0089058d69b5d6eb6fc4e65277ee4cd4e67a
BLAKE2b-256 checksum
How to use checksums
bfb823fbe4b4b85702fd9f4b55f342f3b59dfe26a554a4350fa79f527d0fd7c6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.2

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

This release

0.1.0 This release

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

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