9
$\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

11
$\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
10
$\begingroup$
Cases[{_, __}] @ lis 
{{a, b, c}, {e, f}, {h, i, j}} 
$\endgroup$
5
$\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$
4
$\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$ Commented Mar 13, 2024 at 0:21
  • $\begingroup$ Thanks, @eldo ;) $\endgroup$ Commented Mar 13, 2024 at 0:22

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.