3
$\begingroup$

I have a nested list:

{{{1, 2, 3, 4, 5, 6}}, {{1}, {2, 3, 4, 5, 6}}, {{1, 2}, {3}, {4, 5, 6}}}

and would like to select only those lists out of it with two items in it:

{{{1}, {2, 3, 4, 5, 6}}}

How could this be achieved?

$\endgroup$
3
  • 2
    $\begingroup$ Select[lst, Length@# == 2 &] (see Select) $\endgroup$
    – kglr
    Commented Jun 19, 2017 at 21:49
  • 2
    $\begingroup$ Cases[lst,{_,_}] $\endgroup$
    – andre314
    Commented Jun 19, 2017 at 21:58
  • $\begingroup$ Select[EqualTo[2]@*Length][data] $\endgroup$
    – Syed
    Commented Apr 25 at 17:34

1 Answer 1

2
$\begingroup$

Input

data = {{{1, 2, 3, 4, 5, 6}}, {{1}, {2, 3, 4, 5, 6}}, {{1, 2}, {3}, {4, 5, 6}}};

Select[data, Length @ # == 2 &] 

Output

{{{1}, {2, 3, 4, 5, 6}}}
$\endgroup$
2
  • $\begingroup$ thanks! very helpful $\endgroup$
    – Wafram
    Commented Jul 14, 2017 at 18:46
  • $\begingroup$ You are welcome :) $\endgroup$ Commented Jul 15, 2017 at 22:33

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.