0
$\begingroup$

I have the following list:

l = {{-5, -5, -5}, {-7, -5, -1}, {-9, -3, 15}, {-11, 1, 49}, {-13, 7, 0},
{-15, 15, 0}, {-17, 25, 0}, {-19, 37, 0}, {-21, 51, 0}, {-23, 67, 0}, {-25, 85, 0}}

I need to remove the items in the list that contain zero in the third item value. Finally I wish to obtain the following list:

{{-5, -5, -5}, {-7, -5, -1}, {-9, -3, 15}, {-11, 1, 49}}

$\endgroup$

3 Answers 3

5
$\begingroup$
l = {{-5, -5, -5}, {-7, -5, -1}, {-9, -3, 15}, {-11, 1, 49}, {-13, 7, 
0}, {-15, 15, 0}, {-17, 25, 0}, {-19, 37, 0}, {-21, 51, 0}, {-23, 
67, 0}, {-25, 85, 0}};

DeleteCases[l, {a_, b_, c_} /; c == 0]
$\endgroup$
3
  • 8
    $\begingroup$ Simpler is DeleteCases[l, {_, _, 0}] $\endgroup$
    – Carl Woll
    Apr 19, 2018 at 2:05
  • $\begingroup$ It depends if you want to check for exact zeroes (0) only or if you want to catch machine precision zeroes as well (0.) $\endgroup$ Apr 19, 2018 at 8:47
  • $\begingroup$ Or with Cases[l, {a_, b_, c_} /; c != 0] $\endgroup$ Apr 19, 2018 at 11:08
1
$\begingroup$
Select[l, #[[3]] != 0 &]

{{-5, -5, -5}, {-7, -5, -1}, {-9, -3, 15}, {-11, 1, 49}}

$\endgroup$
1
$\begingroup$

this should be quite fast:

Pick[l, Unitize@l[[All, -1]], 1]

(* {{-5, -5, -5}, {-7, -5, -1}, {-9, -3, 15}, {-11, 1, 49}} *)
$\endgroup$

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