8
$\begingroup$

I have a list:

lis = {{a,b,c},{d},{e,f},{g},{h,i,j}}

I would like to remove each element that consists of only one subelement from the list to get:

res = {{a,b,c},{e,f},{h,i,j}}

This seems to be for SequenceReplace, but I'm having trouble with structuring the command.

$\endgroup$

4 Answers 4

10
$\begingroup$

Cases[lis, Except[{_}]] should be good.

OR

Select[lis, Length[#] > 1 &]

Pick[lis, Length[#] > 1 & /@ lis]

DeleteCases[lis, {_}]

lis /. {_} -> Nothing

EDIT a few more

Select[lis, Rest[#] != {} &]

Select[lis, Most[#] != {} &]

Select[lis, Last@TakeDrop[#, 1] != {} &]
$\endgroup$
1
  • $\begingroup$ Cases, DeleteCases, and Select can all be used here. But as a purely aesthetic point, I would prefer to use either Cases or Select so that I can know with certainty the structure of the elements that I have retained in the next step. With DeleteCases, I would need to use the structure up to that point, and the pattern I have chosen to delete, in order to come to a conclusion about the structure of the result. $\endgroup$ Commented Oct 7, 2021 at 17:17
9
$\begingroup$
Cases[{_, __}] @ lis
{{a, b, c}, {e, f}, {h, i, j}}
$\endgroup$
4
$\begingroup$
list = {{a, b, c}, {d}, {e, f}, {g}, {h, i, j}};

Using SequenceSplit (new in 11.3)

Catenate @ SequenceSplit[list, {{_}}]

{{a, b, c}, {e, f}, {h, i, j}}

$\endgroup$
3
$\begingroup$
list = {{a, b, c}, {d}, {e, f}, {g}, {h, i, j}};

Using GroupBy:

GroupBy[list, Length@# > 1 &][True]

(*{{a, b, c}, {e, f}, {h, i, j}}*)

Or using Replace at level 1:

Replace[list, s : {_} :> Nothing, {1}]

(*{{a, b, c}, {e, f}, {h, i, j}}*)
$\endgroup$
2
  • 1
    $\begingroup$ I always forget these nice GroupBy solutions :) $\endgroup$
    – eldo
    Commented Mar 13 at 0:21
  • $\begingroup$ Thanks, @eldo ;) $\endgroup$ Commented Mar 13 at 0:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.