# deleting entire row if certain element is zero

Suppose, I have a list-

   data = {{1, 2, 3}, {3, 5, 0}, {8, 9, 3}, {2, 5, 0}};


I want to delete second and fourth sublists for which third element of the row is zero. I can do this using

   DeleteCases[data,{_,_,0}];


How can the same operation be achieved efficiently if the list contains a large number of elements instead of just 3?

• That depends on where the zero is going to be. E.g. the third element, the last element or some other position. But probably the pattern __ (BlankSequence[]) is what you are after. May 4 '17 at 8:30
• 45429 but use DeleteCases.
– Kuba
May 4 '17 at 8:34
• Maybe not the most efficient, but incredibly convenient: data /. {_, _, 0} -> Nothing
– ktm
May 4 '17 at 13:17

If your data is packed, then the fastest will probably be

Pick[data, Unitize@data[[All, m]], 1]


where m is the position where you don't allow a 0. If it is the last element in a row, then you can use -1 for m.

In:

Clear[unitize, pick, n, data]
RandomSeed;
n = -1;
data = RandomChoice[Range[0, 10], {10^8, 3}];

AbsoluteTiming[Pick[data, Unitize@data[[All, n]], 1] // Length]

unitize[x_] := unitize[x] = Unitize[x]
pick[xs_, sel_, patt_] := pick[xs] = Pick[xs, sel, patt]
AbsoluteTiming[pick[data, unitize@data[[All, n]], 1] // Length]


Out:

{7.3081, 90913401}
{5.87919, 90913401}

• -1, this doesn't answer the question. The solution would be fine if instead of AnyTrue you had nthTrue, but nthTrue is not a built-in. May 4 '17 at 9:50
• Fair enough. I optimised it base on Marius's method. May 4 '17 at 12:49
• un-downvoted, but this is essentially identical to the accepted answer + some very odd memoization technique, which I'm not sure what is achieving here. You know, your original approach would have easily been doable as Select[data, #[[n]] != 0 &]. As an aside, I see you using memoization in many of your recent answers, why do you utilize it so heavily? May 4 '17 at 13:57
• In Functional Programming, we call it Pure Function, It's NOT Pure Function in Wolfram Language. Pure Functions in Wolfram Language is just anonymous functions. When I know that a function has no side effect, such as File System, Network, I implement it as a Pure Function(Functional Programming term). May 4 '17 at 14:08
• The pure function always evaluates the same result value given the same argument value(s). The function result value cannot depend on any hidden information or state that may change while program execution proceeds or between different executions of the program, nor can it depend on any external input from I/O devices (usually—see below). Evaluation of the result does not cause any semantically observable side effect or output, such as mutation of mutable objects or output to I/O devices. en.wikipedia.org/wiki/Pure_function May 4 '17 at 14:08