Say I have a dataset like
(1, 2, (3, 4), (5, 6), (7, 8, (9, 0)))
I want to convert it to a (semi) flat representation like,
(
(1, 2),
(1, 2, 3, 4),
(1, 2, 5, 6),
(1, 2, 7, 8),
(1, 2, 7, 8, 9, 0),
)
If you use this, (taken from SO)
def flatten(iterable):
for i, item in enumerate(iterable):
if hasattr(item, '__iter__'):
for nested in flatten(item):
yield nested
else:
yield item
this will convert it to a list like(after iterating)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
But I cant get the original from this reperenstation, while I can get the original back from the first. (If every tuple has 2 elements only)