23
$\begingroup$

This question already has an answer here:

I have nested lists wherein there are a lot of waste levels which looks like

l = {{{{{{2, 2}}, 3}, 2, {{2, 33}}, 4, 5}}};

I would like to reduce it to somthing like

l = {{{2, 2}, 3}, 2, {2, 33}, 4, 5};

I made the function which remove waste levels on the top level:

l = {{{{{{2, 2}}, 3}, 2, {{2, 33}}, 4, 5}}};

strip = Function[{list},
                 If[(Head[list] === List) && (Length[list] > 0),
                    If[Flatten[list, 1] === list[[1]], 
                       s = strip[list[[1]]], 
                       s = list],
                    s = list];
                 s];
strip[l]

To do it at any depth I tried to do Map[strip[l]] without success. How I can to apply my function to all levels? Also, I guess there more appropriate way to solve my problem.

$\endgroup$

merged by Mr.Wizard Feb 25 '17 at 21:41

This question was merged with How to remove redundant {} from a nested list of lists? because it is an exact duplicate of that question.

Browse other questions tagged or ask your own question.