Skip to main content

performs customizable level-wise flattening of nested iterables.

pip install levelflatten

Tested against Windows 10 / Python 3.10 / Anaconda

The level_flatten function provides the following advantages:

Customizable flattening:

It allows you to control the flattening behavior for different data types. You can specify how dictionaries should be treated (by items, keys, or values) and which non-iterable data types should be considered for individual element flattening.

Level-wise flattening:

It performs flattening in a level-wise manner, allowing you to control the depth of flattening. You can specify the maximum number of levels to flatten, which provides flexibility in handling complex nested structures.

Support for various data types:

It handles various data types such as lists, tuples, sets, dictionaries, strings, bytes, frozensets, bytearrays, NumPy arrays, and more. This makes it suitable for working with diverse data structures.

Maintains order:

The function preserves the order of elements while flattening, ensuring that the relative order of elements is maintained in the flattened list.

Args:
 iterable (iterable): The input iterable to be flattened.
 n (int, optional): The maximum number of levels to flatten. Defaults to sys.maxsize.
 dict_treatment (str, optional): Treatment for dictionaries. Can be one of "items",
	 "keys", or "values". Defaults to "items".
 consider_non_iter (tuple or list, optional): Tuple or list of data types to be
	 considered non-iterable and flattened as individual elements. Defaults to
	 (str, bytes).

Returns:
 list: A flattened list containing the elements from the input iterable.
 
 
 from levelflatten import level_flatten
 import numpy as np
 from collections import defaultdict

 d = defaultdict(list)
 d[1].append((1, 2, 3, [44, 5, 5]))
 iti = [
	 {(1, 2), (3, 4)},
	 ["a", 111, 3],
	 (1, 2),
	 d,
	 "stristristri",
	 range(100, 110),
	 455j,
	 b"xxxxaaa",
	 frozenset({1, 2, 3, 4}),
	 [
		 11,
		 [
			 222,
			 33,
			 4,
			 (333, 4, {1, 2, 3, 4}, {3333: 333}, [33312, 33323], bytearray(b"xxxxx")),
		 ],
	 ],
	 [
		 {2: 3},
		 (
			 2,
			 3,
			 4,
		 ),
		 ["babab", "dd", {10: 12}, (("bbb", 12), [333, 4, {4: 32}])],
	 ],
	 (1.2, 11, 1232),
	 bytearray(b"xxxxx"),
	 np.array([1, 2, 3, 4, 5]),
	 [None, np.nan],
	 [[True, False, True]],
 ]

 t1 = level_flatten(
	 iti,
	 n=1,
	 dict_treatment="items",
	 consider_non_iter=(str, bytes, frozenset, bytearray),
 )
 print(t1)
 # [(1, 2), (3, 4), 'a', 111, 3, 1, 2, (1, [(1, 2, 3, [44, 5, 5])]), 'stristristri', 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 455j, b'xxxxaaa', frozenset({1, 2, 3, 4}), 11, [222, 33, 4, (333, 4, {1, 2, 3, 4}, {3333: 333}, [33312, 33323], bytearray(b'xxxxx'))], {2: 3}, (2, 3, 4), ['babab', 'dd', {10: 12}, (('bbb', 12), [333, 4, {4: 32}])], 1.2, 11, 1232, bytearray(b'xxxxx'), 1, 2, 3, 4, 5, None, nan, [True, False, True]]

 t1 = level_flatten(
	 iti, n=2, dict_treatment="values", consider_non_iter=(str, bytes, dict)
 )
 print(t1)
 # [1, 2, 3, 4, 'a', 111, 3, 1, 2, (1, 2, 3, [44, 5, 5]), 'stristristri', 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 455j, b'xxxxaaa', 1, 2, 3, 4, 11, 222, 33, 4, (333, 4, {1, 2, 3, 4}, {3333: 333}, [33312, 33323], bytearray(b'xxxxx')), {2: 3}, 2, 3, 4, 'babab', 'dd', {10: 12}, (('bbb', 12), [333, 4, {4: 32}]), 1.2, 11, 1232, 120, 120, 120, 120, 120, 1, 2, 3, 4, 5, None, nan, True, False, True]


 t1 = level_flatten(
	 iti, n=3, dict_treatment="keys", consider_non_iter=(str, bytes, set, tuple)
 )
 print(t1)
 # [{(1, 2), (3, 4)}, 'a', 111, 3, (1, 2), 1, 'stristristri', 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 455j, b'xxxxaaa', 1, 2, 3, 4, 11, 222, 33, 4, (333, 4, {1, 2, 3, 4}, {3333: 333}, [33312, 33323], bytearray(b'xxxxx')), 2, (2, 3, 4), 'babab', 'dd', 10, (('bbb', 12), [333, 4, {4: 32}]), (1.2, 11, 1232), 120, 120, 120, 120, 120, 1, 2, 3, 4, 5, None, nan, True, False, True]


 t1 = level_flatten(
	 iti,
	 n=4,
	 dict_treatment="items",
	 consider_non_iter=(str, bytes, set, tuple, dict, bytearray),
 )
 print(t1)
 # [{(1, 2), (3, 4)}, 'a', 111, 3, (1, 2), (1, [(1, 2, 3, [44, 5, 5])]), 'stristristri', 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 455j, b'xxxxaaa', 1, 2, 3, 4, 11, 222, 33, 4, (333, 4, {1, 2, 3, 4}, {3333: 333}, [33312, 33323], bytearray(b'xxxxx')), {2: 3}, (2, 3, 4), 'babab', 'dd', {10: 12}, (('bbb', 12), [333, 4, {4: 32}]), (1.2, 11, 1232), bytearray(b'xxxxx'), 1, 2, 3, 4, 5, None, nan, True, False, True]


 t1 = level_flatten(
	 iti, n=sys.maxsize, dict_treatment="items", consider_non_iter=(np.array, np.ndarray)
 )
 print(t1)
 # [1, 2, 3, 4, 'a', 111, 3, 1, 2, 1, 1, 2, 3, 44, 5, 5, 's', 't', 'r', 'i', 's', 't', 'r', 'i', 's', 't', 'r', 'i', 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 455j, 120, 120, 120, 120, 97, 97, 97, 1, 2, 3, 4, 11, 222, 33, 4, 333, 4, 1, 2, 3, 4, 3333, 333, 33312, 33323, 120, 120, 120, 120, 120, 2, 3, 2, 3, 4, 'b', 'a', 'b', 'a', 'b', 'd', 'd', 10, 12, 'b', 'b', 'b', 12, 333, 4, 4, 32, 1.2, 11, 1232, 120, 120, 120, 120, 120, array([1, 2, 3, 4, 5]), None, nan, True, False, True]

Metadata

Release files for levelflatten 0.10

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

Source distribution (sdist)

Source distribution for levelflatten 0.10
File Size Uploaded Provenance Actions
levelflatten-0.10.tar.gz 6.0 kB Without provenance Details for levelflatten-0.10.tar.gz

Built distribution (wheel)

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

Total release size: 14.2 kB

Release files / levelflatten-0.10.tar.gz

Download URL levelflatten-0.10.tar.gz
Size 6.0 kB
Tags Source
SHA-256 checksum
How to use checksums
a7f5876e3f3b95be4eed0f6e4d4314e8ff7fbdb6720451e6fb0a1bd017a3a100
BLAKE2b-256 checksum
How to use checksums
eab24e0caf9c1941c40b8385cc0f3ff4fc1d79c3131d438cabd0a31d4d922c20
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.10

Release files / levelflatten-0.10-py3-none-any.whl

Download URL levelflatten-0.10-py3-none-any.whl
Size 8.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
734d4978caaffb34b889c92cdd0601c3c8821ebf9b1ea7011d1c23da7f3ffc05
BLAKE2b-256 checksum
How to use checksums
8be4bd80ce6726f36fdd6cfefb7bfef23b0d1386d698b48078af4d6a8cce87c1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.10

Security for levelflatten 0.10

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.10 This release

2 release files No provenance

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