Skip to main content

Flatten It

Introduction

Flatten It uses iteration (no recursion) to flatten iterables into a dictionary where each key is a reference path and each value is the value at the respective path.

Features

  • Produces valid Python or JS reference paths for supported string and numeric keys.
  • Raises ValueError on circular references.
  • Iterative algorithm; flatten deeply nested structures without causing a call stack overflow.

Table of contents

Installation

pip install flatten_iterables

Usage

In this example, an object named data will be flattened into a dictionary of reference paths and their respective values. The resulting dictionary will be dumped to a JSON string.

import json
from flatten_iterables import fi

fi.key_style = "python"

data = {
    "dict": {"a": 42},
    "nested_dicts": {"a0": {"b0": 42, "b1": 23}},
    "empty_dict": {},
    "list": [42],
    "nested_lists": [
        [
            42,
        ],
    ],
    "empty_list": [],
    ...: 42,
}

print(json.dumps(fi.flatten(data), indent=2))
{
  "['dict']['a']": 42,
  "['nested_dicts']['a0']['b0']": 42,
  "['nested_dicts']['a0']['b1']": 23,
  "['empty_dict']": {},
  "['list'][0]": 42,
  "['nested_lists'][0][0]": 42,
  "['empty_list']": [],
  "[Ellipsis]": 42
}

By default Flatten It will flatten structures that contain instances of list and dict. Other iterable and mapping types are supported by adding their respective types to the iterables and mappables sets. Registered iterable types must support both iteration and len(). Iterables such as sets are traversed in their normal iteration order, which is not guaranteed to be stable.

In this example, a structure containing the types set and OrderedDict will be flattened. The type set is added to the iterables set and the type OrderedDict is added to the mappables set.

import json
from flatten_iterables import fi
from collections import OrderedDict

fi.iterables.add(set)
fi.mappables.add(OrderedDict)

data = {
    "set": {23, 42, 57},
    "ordered_dict": OrderedDict(a=23, b=42, c=57),
}

print(json.dumps(fi.flatten(data), indent=2))
{
  "['set'][0]": 57,
  "['set'][1]": 42,
  "['set'][2]": 23,
  "['ordered_dict']['a']": 23,
  "['ordered_dict']['b']": 42,
  "['ordered_dict']['c']": 57
}

API

fi.flatten(it)

  • it Union[Iterable, Mapping] The configured iterable or mapping to be flattened. Raises TypeError for unsupported root values.

fi.key_style Literal["python", "js"] Specify a key style. Default: python

fi.iterables Set[Iterable] Add iterable candidates to this set. Default: {list}

fi.mappables Set[Mapping] Add mappable candidates to this set. Default: {dict}

Test

How to run the test

Clone the repository.

git clone https://github.com/adamjpatterson/flatten_iterables.git

Change directory into the root of the repository.

cd flatten_iterables

Install Hatch.

python -m pip install hatch

Run the tests.

hatch test

Metadata

Release files for flatten_iterables 1.0.6

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_iterables 1.0.6
File Size Uploaded
flatten_iterables-1.0.6.tar.gz 6.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flatten_iterables 1.0.6
File Interpreter ABI Platform
flatten_iterables-1.0.6-py3-none-any.whl Python 3 none any Details

Total release size: 10.9 kB

Release files / flatten_iterables-1.0.6.tar.gz

Download URL flatten_iterables-1.0.6.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
054f0d89fa2c9f69f66421b5fd6bc24e89955c93b2acbd542819f06b3296267a
BLAKE2b-256 checksum
How to use checksums
8b86a2c6237686f3bdf6cd2b5f94468f5707532615694cd0e26be744a4fba422
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.28.1

Release files / flatten_iterables-1.0.6-py3-none-any.whl

Download URL flatten_iterables-1.0.6-py3-none-any.whl
Size 4.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9e9d4f25a9e5826962a2ee80eaa0199b33e445b39ebcb679d816145a39b33a2e
BLAKE2b-256 checksum
How to use checksums
a71ce11ca0b2ddca8703eb320491db3db83928e7b833303d9d55863cfbc3625f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.28.1

Release history Release notifications | RSS feed

This release

1.0.6 This release

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

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