I have lists of sets of variables, some in nested sub-lists:
t = {{1, 2}, {3, 4}, {{5, 6}, {7, 8, 9}}}
I need to flatten this list in place but preserve the sets. Flatten[t,1] does the opposite of what I want:
t = Flatten[t,1]
(* {1, 2, 3, 4, {5, 6}, {7, 8, 9}} *)
The following code does what I want if there's only one level of flattening to do. However, it requires a lot of copying, and I'm working with list of >10^5 elements, so I'd like to do this more efficiently if possible.
t = Reap[Do[If[Length[t[[i, 1]]] == 0, Sow[t[[i]]], Do[Sow[t[[i, j]]], {j, 1, Length[t[[i]]]}]], {i, 1, Length[t]}]][[2, 1]]
{{1, 2}, {3, 4}, {5, 6}, {7, 8, 9}}
Is there a more efficient way?
Levelsolution only a few days ago so it was still fresh in my mind! – Quantum_Oli Mar 10 '17 at 14:40