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

2 Answers 2

6
$\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$ Oct 7, 2021 at 17:17
8
$\begingroup$
Cases[{_, __}] @ lis
{{a, b, c}, {e, f}, {h, i, j}}
$\endgroup$

Your Answer

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

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