Update:
2022/09/30: fixed ProtectedDict, ProtectedList, ProtectedTuple, ProtectedSet - Sometimes didn't protect!
2022/09/30: Added doc strings
What does it do?
It flattens any iterable, it doesn't matter how deeply it is nested. If there are dicts in you iterable, it will only return the values. If you need the keys too, have a look at this package: flatten-any-dict-iterable-or-whatsoever · PyPI
Install it:
pip install flatten-everything
Import it:
from flatten_everything import flatten_everything, ProtectedDict,ProtectedList,ProtectedTuple,ProtectedSet
Use it:
{
"id": "001",
"company": "XYZ pvt ltd",
"location": "London",
"info": {
"president": "Rakesh Kapoor",
"contacts": {"email": "contact@xyz.com", "tel": "9876543210"},
"onemorefortesting": {
"name": {"name": "John", "age": "27", "sex": "Male"},
"Peter2": {"name": "Marie", "age": "22", "sex": "Female"},
"sdfsdf": {"name": "Luna", "age": "24", "sex": "Female"},
"another_nested_something": [(2, 1), (3, 2), (1, 2), (1, 3), (1, 3), (2, 3), (1, 1), (3, 3), (2, 1), (1, 1), (1, 2), (3, 1), (3, 1), (3, 2), (1, 2), (1, 1), (3, 2), (2, 1), (1, 1), (3, 1)],
"Peter": {"name": "Peter", "age": "29", "sex": "Male"},
},
},
},
{
"id": "002",
"company": "PQR Associates",
"location": "Abu Dhabi",
"info": {
"president": "Neelam Subramaniyam",
"contacts": {"email": "contact@pqr.com", "tel": "8876443210"},
},
},
]
list(flatten_everything(data))
Result:
['001', 'XYZ pvt ltd', 'London', 'Rakesh Kapoor', 'contact@xyz.com', '9876543210', 'John', '27', 'Male', 'Marie', '22', 'Female', 'Luna', '24', 'Female', 2, 1, 3, 2, 1, 2, 1, 3, 1, 3, 2, 3, 1, 1, 3, 3, 2, 1, 1, 1, 1, 2, 3, 1, 3, 1, 3, 2, 1, 2, 1, 1, 3, 2, 2, 1, 1, 1, 3, 1, 'Peter', '29', 'Male', '002', 'PQR Associates', 'Abu Dhabi', 'Neelam Subramaniyam', 'contact@pqr.com', '8876443210']
#If you want to protect iterables from being flattened, you can use:
data = [
{
"id": "001",
"company": "XYZ pvt ltd",'protect_test':ProtectedTuple((333,332,555)),
"location": "London",
"info": {
"president": "Rakesh Kapoor",
"contacts": {"email": "contact@xyz.com", "tel": "9876543210"}, 'onemorefortesting': {
"name": {"name": "John", "age": "27", "sex": "Male"},
"Peter2": {"name": "Marie", "age": "22", "sex": "Female"},
"sdfsdf": {"name": "Luna", "age": "24", "sex": "Female"}, 'another_nested_something': ProtectedList([(2, 1), (3, 2), (1, 2), (1, 3), (1, 3), (2, 3), (1, 1), (3, 3), (2, 1), (1, 1), (1, 2), (3, 1), (3, 1), (3, 2), (1, 2), (1, 1), (3, 2), (2, 1), (1, 1), (3, 1)]),
"Peter": ProtectedDict({"name": "Peter", "age": "29", "sex": "Male"}),
},},},{"id": "002",
"company": "PQR Associates",
"location": "Abu Dhabi",
"info": { "president": "Neelam Subramaniyam",
"contacts": {"email": "contact@pqr.com", "tel": "8876443210"},},},]
print(list(flatten_everything(data)))
['001', 'XYZ pvt ltd', (333, 332, 555), 'London', 'Rakesh Kapoor', 'contact@xyz.com', '9876543210', 'John', '27', 'Male', 'Marie', '22', 'Female', 'Luna', '24', 'Female', [(2, 1), (3, 2), (1, 2), (1, 3), (1, 3), (2, 3), (1, 1), (3, 3), (2, 1), (1, 1), (1, 2), (3, 1), (3, 1), (3, 2), (1, 2), (1, 1), (3, 2), (2, 1), (1, 1), (3, 1)], {'name': 'Peter', 'age': '29', 'sex': 'Male'}, '002', 'PQR Associates', 'Abu Dhabi', 'Neelam Subramaniyam', 'contact@pqr.com', '8876443210']
#Parameters:
# item: Any
# Input iterable
# Most of the time you will be using only this parameter.
# forbidden: tuple
# Data dtype which cannot be returned
# (default=(list, tuple, set, frozenset))
# allowed: tuple
# Data dtype which can be returned
# default (
# str,
# int,
# float,
# complex,
# bool,
# bytes,
# type(None),
# ProtectedTuple, # Inherits from tuple but is protected, this is how you protected iterables
# ProtectedList, # same here
# ProtectedDict, # same here
# ProtectedSet, # same here
# Tuppsub #Inherit from tuple and exclude it from being flattened -
#
# )
# dict_variation: tuple
# Due to recent changes, might not be necessary anymore, used to filter dict variations
# (default =
# (
# "collections.defaultdict",
# "collections.UserDict",
# "collections.OrderedDict",
# )
#Returns:
# Generator
Metadata
Release files for flatten-everything 0.41
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| flatten_everything-0.41.tar.gz | 6.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| flatten_everything-0.41-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 15.4 kB
Release files / flatten_everything-0.41.tar.gz
| Download URL | flatten_everything-0.41.tar.gz |
|---|---|
| Size | 6.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
81918e9a1d0a7131f0bcab8fbb91a234728da6d536497befe51d5a6dbdcca507
|
|
BLAKE2b-256 checksum How to use checksums |
84f7856d3633c3edefe1c6db9b066f22e525d2f1f689ae08be4d52153f1f2fbb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.13
|
Release files / flatten_everything-0.41-py3-none-any.whl
| Download URL | flatten_everything-0.41-py3-none-any.whl |
|---|---|
| Size | 8.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9dd6623e230d8a79494d769aa5821186b0e9ff8da5bbae6e8043a8f69575b11a
|
|
BLAKE2b-256 checksum How to use checksums |
d04ae37e9657d07cbb8d876b0a9d1745080cbe8491a502f3bde1cc2fecfd2037
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.13
|