Skip to main content

Python module for interacting with nested dicts as a single level dict with delimited keys.

Project description

Version Status Coverage

FlatDict is a dict object that allows for single level, delimited key/value pair mapping of nested dictionaries. You can interact with FlatDict like a normal dictionary and access child dicts as you normally would or with the composite key.

Examples

foo = {'foo': {'bar': 'baz', 'qux': 'corge'}}

is represented as:

{'foo:bar': 'baz',
 'foo:qux': 'corge'}

And can still be accessed as:

foo['foo']['bar']

and

foo['foo:bar']

Additionally, lists and tuples are also converted into dicts using enumerate().

For example:

d = {'list': ['a', 'b', 'c',]}

Will be flattened as follows:

flat = {'list:0': 'a', 'list:1': 'b', 'list:2': 'c'}

API

Documentation is available at https://flatdict.readthedocs.io

Installation

$ pip install flatdict

Example Use

import pprint

import flatdict

values = {'foo': {'bar': {'baz': 0,
                          'qux': 1,
                          'corge': 2},
                  'grault': {'baz': 3,
                             'qux': 4,
                             'corge': 5}},
          'garply': {'foo': 0, 'bar': 1, 'baz': 2, 'qux': {'corge': 3}}}

flat = flatdict.FlatDict(values)

print(flat['foo:bar:baz'])

flat['test:value:key'] = 10

del flat['test']

for key in flat:
    print(key)

for value in flat.itervalues():
    print(value)

pprint.pprint(flat.as_dict())

pprint.pprint(dict(flat))

print(flat == flat.as_dict())

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
flatdict-3.1.0-py2.py3-none-any.whl (6.6 kB) Copy SHA256 hash SHA256 Wheel py2.py3
flatdict-3.1.0.tar.gz (6.7 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page