@march has already given the answer, but neglected to write one formally. There are multiple ways to do this, but the one that makes most sense to me is this:
Select[lis, (Length[#]==2) &]
I'm going to guess that you're not familiar with some of the syntax here, so I'll break it down a bit.
Select
is a function taking in a list follow by a predicate to be applied to elements of the list. A predicate is a function of a single argument which outputs either True
or False
. The Select
method will apply the predicate to each of the elements of the list, and remove those for whom the output is False
. Note that it doesn't alter the original list; it just outputs a new one.
Here, the predicate is a pure function: (Length[#] == 2)&
. The #
is replaced with the first argument that this function is applied to, so for each element el
in the list lis
, Select
is checking to see if Length[el]==2
is True
. If it is, then it remains in the list.
We could make a function which takes in the desired sublist length and returns the appropriate predicate:
filterLength[n_]:=(Length[#]==n)&;
Full code:
filterLength[n_]:=(Length[#]==n)&;
lis = {a, b, {c, d}, e, {f, g}, {h, j, k}, m, n};
newLis0 = Select[lis,filterLength[0]];
newLis2 = Select[lis,filterLength[2]];
newLis3 = Select[lis,filterLength[3]];
Print[newLis0]; Print[newLis2]; Print[newLis3];
Output:
{a,b,e,m,n}
{{c,d},{f,g}}
{{h,j,k}}
Notice that order is preserved.
Cases
. $\endgroup$Cases[lis, {_, _}]
.Select[lis, Length@# == 2 &]
.Replace[lis, Except[{_, _}] :> (## &[]), 1]
. $\endgroup${a, b, {c, d}, e, {f, g}, {h, j, k}, m, n, {o, p, q}}
$\endgroup$DeleteCases[Except[{_, _}]][lis]
$\endgroup$