Skip to main content

A flexible utility for flattening and unflattening dict-like objects in Python.

Project description

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):
    """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'}

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', function} (default: 'tuple')
        The key splitting method. If a function is given, the function 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 (default: False)
        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'}}}

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

flatten-dict-0.0.3.post1.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

flatten_dict-0.0.3.post1-py2.py3-none-any.whl (5.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file flatten-dict-0.0.3.post1.tar.gz.

File metadata

File hashes

Hashes for flatten-dict-0.0.3.post1.tar.gz
Algorithm Hash digest
SHA256 e3696310519c6d16209288c1c04d8249447b8ea4de83d3f08d6b9bbdc3981f71
MD5 e8a5066e3376b742a4ee3587f251fafa
BLAKE2b-256 0868eaeb54b9d707701d289698b30753cd1e08aec43ac384beefa91dccdcfe34

See more details on using hashes here.

File details

Details for the file flatten_dict-0.0.3.post1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for flatten_dict-0.0.3.post1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 2ee6b3fa263e6404cf2dc3827d9ad11c863268b4f02f80bb6edfc2241cd95ab9
MD5 ed42dacca488d7df6a8668c6b426c145
BLAKE2b-256 7fc2a3427d69137de28e3c83bcf0244925542dae7402e26c574d48b304d1f460

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page