4
$\begingroup$

I have a list of strings:

  lis = {{"w", "u"}, {"q", "r"}, {"z"}, {"d", "e"}, {"f"}, {"g", "h"}, {"o"}}

I would like to delete all single-element members of this list to give:

  res = {{"w","u"}, {"q","r"}, {"d","e"}, {"g","h"}}

  SequenceCases[lis, {{a_, b_}, {_}, {c_, d_}} :> {{a, b}, {c, d}}, Overlaps -> True]

fails. Apologies for this simple question!

$\endgroup$
3
  • 1
    $\begingroup$ res = DeleteCases[lis, _List?(Length[#] < 2 &)] $\endgroup$
    – Bob Hanlon
    Sep 19, 2023 at 4:11
  • 1
    $\begingroup$ DeleteCases[lis, {_}] $\endgroup$
    – Syed
    Sep 19, 2023 at 4:12
  • 1
    $\begingroup$ Cases[lis,{x_,y_}] $\endgroup$
    – user1066
    Sep 19, 2023 at 4:17

3 Answers 3

2
$\begingroup$

Avoiding patterns:

Select[lis, Length[#] > 1 &]
{{w,u},{q,r},{d,e},{g,h}}
$\endgroup$
2
$\begingroup$

Another way to do this using ReplaceAll:

ReplaceAll[lis, x : {_} :> Nothing]

{{"w", "u"}, {"q", "r"}, {"d", "e"}, {"g", "h"}}

Also, using Cases:

Cases[lis, x : {__} /; Length[x] > 1]

{{"w", "u"}, {"q", "r"}, {"d", "e"}, {"g", "h"}}
$\endgroup$
2
$\begingroup$

Since your try with SequenceCases failed

Join @@ SequenceCases[lis, {{_, _} ..}]

{{"w", "u"}, {"q", "r"}, {"d", "e"}, {"g", "h"}}

Or

SequenceReplace[{{_} ..} :> Nothing] @ lis

{{"w", "u"}, {"q", "r"}, {"d", "e"}, {"g", "h"}}

In addition to the answers already given better candidates would include

Cases[{_, _}] @ lis

{{"w", "u"}, {"q", "r"}, {"d", "e"}, {"g", "h"}}

and

 Pick[lis, Length /@ lis, 2]

{{"w", "u"}, {"q", "r"}, {"d", "e"}, {"g", "h"}}

$\endgroup$

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.