4
$\begingroup$

I have a list in the form

{{A,B,False},{D,C,True},{E,F,True},{G,H,False}}

I want to delete all the sublists containing False. That is, after the applying the function I would like to have the list

{{D,C,True},{E,F,True}}

I've tried using DeleteCases, but I can't get it to work. Thanks for any help.

$\endgroup$
4
  • $\begingroup$ C, D and E have reserved meaning in Mathematica. $\endgroup$
    – Syed
    Aug 2, 2023 at 11:57
  • $\begingroup$ I know, but the list that I have have basically arbitrary data (string, functions etc.). $\endgroup$
    – Nitaa a
    Aug 2, 2023 at 12:00
  • 1
    $\begingroup$ Also just for this particular case: Pick[#,#[[All,-1]]]&@lst $\endgroup$
    – user1066
    Aug 2, 2023 at 22:09
  • 1
    $\begingroup$ Pick[list, Last /@ list] $\endgroup$ Aug 3, 2023 at 22:21

5 Answers 5

8
$\begingroup$

use DeleteCases

{{A,B,False},{D,C,True},{E,F,True},{G,H,False}} // DeleteCases[{___, False, ___}]

{{D, C, True}, {E, F, True}}

use Select

{{A,B,False},{D,C,True},{E,F,True},{G,H,False}} // Select[Not @* MemberQ[False]]
$\endgroup$
6
$\begingroup$
Pick[alist, ContainsNone[{False}][#] & /@ alist]

Pick[alist, ContainsAny[{False}][#] & /@ alist, False]

DeleteCases[alist, _?(ContainsAny[{False}])]

Cases[alist, _?(FreeQ[False])]

{{D, C, True}, {E, F, True}}

$\endgroup$
6
$\begingroup$

In this particular case:

list = {{a, b, False}, {d, c, True}, {e, f, True}, {g, h, False}};

Select[Last] @ list

{{d, c, True}, {e, f, True}}

$\endgroup$
4
$\begingroup$

Another way using GroupBy:

lst = {{A,B,False},{D,C,True},{E,F,True},{G,H,False}};

Last@GroupBy[lst, MemberQ[False]]

(*{{D, C, True}, {E, F, True}}*)

Or using Extract:

Extract[#, Position[#, x_List /; FreeQ[x, False]]] &[lst]

(*{{D, C, True}, {E, F, True}}*)
$\endgroup$
4
$\begingroup$

Using ReplaceAll:

lst = {{a, b, False}, {d, c, True}, {e, f, True}, {g, h, False}};

lst /. {___, False, ___} -> Nothing

{{d, c, True}, {e, f, True}}

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