# Dropping lists using pattern [duplicate]

I have the following list of lists.

 data = {{0, 0, 0}, {1, 0, 0}, {2, 0, 1}, {3, 0, 0}, {4, 0, 0}, {5, 0, 3}}


I want to drop all the lists with 0 as the 3rd entry i.e. {m,n,0}. How can I do this?

Easy (but probably not the fastest) way:

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


{{2, 0, 1}, {5, 0, 3}}

This might be a bit faster for longer lists as it entirely avoids pattern matching:

Delete[data, Partition[RandomPrivatePositionsOf[data[[All, 3]], 0], 1]]


Here a speed test:

data = RandomInteger[{0, 5}, {1000000, 3}];
a = DeleteCases[data, {_, _, 0}]; // AbsoluteTiming // First
b = Delete[data, Partition[RandomPrivatePositionsOf[data[[All, 3]], 0], 1]]; //
AbsoluteTiming // First
a == b


0.407339

0.027375

True

Another way

data = {{0, 0, 0}, {1, 0, 0}, {2, 0, 1}, {3, 0, 0}, {4, 0, 0}, {5, 0, 3}}
Pick[data, Unitize@data[[;; , 3]], 1]


{{2, 0, 1}, {5, 0, 3}}

And a timing comparison to DeleteCases

SeedRandom[1234]
Block[
{data = RandomInteger[{0, 5}, {1000000, 3}], times, m1, m2},
times = {
AbsoluteTiming[m1 = DeleteCases[data, {__, 0}]][[1]],
AbsoluteTiming[m2 = Pick[#, Unitize@#[[;; , 3]], 1] &@data][[1]]
};
{times, m1 == m2}
]


{{0.403632, 0.031339}, True}