# Restructure a list with brackets

I work with the following list

data = {{a}, {{a, b}, {a, b}}, {{{a, b, c}}}};


In real, the dataset is much longer and each row has a different structure of having {}

I want to restructure it to:

{{a}, {a, b},  {a, b}, {a, b, c}}


Is this possible using Flatten. If not, what is the easiest way to do this.

A special case is when the data contains DateObjects like:

{{{DateObject[01 - 06 - 2020], "buy", 572.18,
DateObject[01 - 06 - 2020], "sell", 570.52}}, {
DateObject[01 - 12 - 2020], "buy",
616.2}, {DateObject[01 - 01 - 2021], "sell",
697.94}, {DateObject[01 - 02 - 2021], "buy",
708.43}, {DateObject[01 - 03 - 2021], "keep", 709.36}}

• Level[data, {-2}] ? Commented Jun 17, 2021 at 13:42
• Cases[data, {Except[_List]..}, Infinity] or Cases[data, _List?VectorQ, Infinity] might be what you want. The suggestion by @BenIzd will only work if the elements of you lists are all atomic. Commented Jun 17, 2021 at 14:13
• You could add your new test case to the question Commented Jun 17, 2021 at 15:49
• Use Replace[ReplaceRepeated[data, {{a__}} :> {a}], {a__List} :> a, {1}] for your new test case. Commented Jun 17, 2021 at 16:45
• also List[data //. {a__List} :> a] and Replace[data, {a__List} :> a, All]?
– kglr
Commented Jun 17, 2021 at 17:10

data = {{a}, {{a, b}, {a, b}}, {{{a, b, c}}}};


Using Cases

Cases[data, {_?AtomQ , ___}, -1]


{{a}, {a, b}, {a, b}, {a, b, c}}

Try this:

Join[Sequence @@ Map[Level[#, {Depth[#] - 2}] &, data]]

(*  {{a}, {a, b}, {a, b}, {a, b, c}}   *)


I am not sure if it will work in more complex cases.

data = {{a}, {{a, b}, {a, b}}, {{{a, b, c}}}};

Cases[data, _?VectorQ, All]


Result:

{{a}, {a, b}, {a, b}, {a, b, c}}

data = {{a}, {{a, b}, {a, b}}, {{{a, b, c}}}};


Using Delete and Nest:

f = Nest[Delete[#, {0}] &, #, Depth[#] - 2] &;

f /@ data

(*{{a}, {a, b}, {a, b}, {a, b, c}}*)
`