I created a little function which can basically flatten anything.
You can get it with pip: pip install flatten-everything
from flatten_everything import flatten_everything
withoutprotection=list(
flatten_everything(
[
1,
1,
2,
[3, 4, 5, [6, 3, [2, 5, ["sfs", "sdfsfdsf",]]]],
1,
3,
34,
[
55,
{"brand": "Ford", "model": "Mustang", "year": 1964, "yearxx": 2020},
pd.DataFrame({"col1": [1, 2], "col2": [3, 4]}),
{"col1": [1, 2], "col2": [3, 4]},
55,
{"k32", 34},
np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]),
(np.arange(22), np.eye(2, 2), 33),
],
]
)
)
print(withoutprotection)
output:
[1, 1, 2, 3, 4, 5, 6, 3, 2, 5, 'sfs', 'sdfsfdsf', 1, 3, 34, 55, 'Ford', 'Mustang', 1964, 2020, 1, 2, 3, 4, 1, 2, 3, 4, 55, 34, 'k32', 1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 1.0, 0.0, 0.0, 1.0, 33]
You can even protect objects from getting flattened:
from flatten_everything import ProtectedDict,ProtectedList,ProtectedTuple
withprotection=list(
flatten_everything(
[
1,
1,
2,
[3, 4, 5, [6, 3, [2, 5, ProtectedList(["sfs", "sdfsfdsf",])]]],
1,
3,
34,
[
55,
ProtectedDict({"brand": "Ford", "model": "Mustang", "year": 1964, "yearxx": 2020}),
pd.DataFrame({"col1": [1, 2], "col2": [3, 4]}),
{"col1": [1, 2], "col2": [3, 4]},
55,
{"k32", 34},
np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]),
ProtectedTuple((np.arange(22), np.eye(2, 2), 33)),
],
]
)
)
print(withprotection)
output:
[1, 1, 2, 3, 4, 5, 6, 3, 2, 5, ['sfs', 'sdfsfdsf'], 1, 3, 34, 55, {'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'yearxx': 2020}, 1, 2, 3, 4, 1, 2, 3, 4, 55, 34, 'k32', 1, 2, 3, 4, 5, 6, 7, 8, (array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,17, 18, 19, 20, 21]), array([[1., 0.], [0., 1.]]), 33)]