# How to delete certain unwanted elements from an array

Suppose I have an array like so

array1 = {{Null,2,3,4,5},
{a,b,c,Null,e},
{w,x,Null,y,z}}

Is there some way to selectively remove any element I want? In this case, I want to remove any Null element so that I get

array2 = {{2,3,4,5},
{a,b,c,e},
{w,x,y,z}}

I attempted this by trying out the Complement command. As an example, I did something to the effect of the following.

array3 = {{1,1,Null},
{2,Null,Null},
{a,a,Null}}

null = {Null}

newarray = Complement[array3,null]

which gives

newarray = {{1},
{2},
{a}}

array1 /. Null -> Sequence[]
(* {{2, 3, 4, 5}, {a, b, c, e}, {w, x, y, z}} *)

Replace[array1, Null :> Sequence[], {2}]
(* {{2, 3, 4, 5}, {a, b, c, e}, {w, x, y, z}} *)

DeleteCases[array1, Null, {2}]
(* {{2, 3, 4, 5}, {a, b, c, e}, {w, x, y, z}} *)

SetAttributes[foo, Listable]
foo[Null] = Sequence[];
foo[x_] := x
foo@array1
(* {{2, 3, 4, 5}, {a, b, c, e}, {w, x, y, z}} *)

A minor variation of @kglr's DeleteCases approach is to use a negative level spec targeting leaves only:

DeleteCases[array1, Null, {-1}]

{{2, 3, 4, 5}, {a, b, c, e}, {w, x, y, z}}

This will do it.

array1 = {{Null, 2, 3, 4, 5}, {a, b, c, Null, e}, {w, x, Null, y, z}};
array1 /. {h___, Null, t___} :> {h, t}

which then gives you

{{2, 3, 4, 5}, {a, b, c, e}, {w, x, y, z}}
• Ah, perfect. I was familiar with the /. symbol but I wasn't sure how to use it properly. EDIT: does the number of underscores set a certain radius for which elements in the list to consider for deletion? Commented Oct 30, 2014 at 3:20
• Buying an old used book that explains how to use some of the features of Mathematica will save you a lot of time. Long ago I learned a lot from reading and rereading "Applied Mathematica: Getting Started, Getting it done." That is where I learned /. and # and & and all that stuff. That is ancient now and used copies are often a dollar. That won't teach you animating your manipulate in the cloud with natural language using subscripts, but it will teach you the fundamentals you will use over and over. Mathematica Navigator older editions are cheap used and would be good too.
– Bill
Commented Oct 30, 2014 at 3:25
• x_ (1 of them) matches a "single thing", but a thing can also be a list, so watch out. x__ (2 of them) matches "one or more things." x___ (3 of them) matches "zero or more of them." So my pattern above was "start a list and then have zero or more things, call those h and then have a Null and then have zero or more things, call those t and end the list, then replace that with the list that is just h followed by t. Now it is time for you to think. What would this do if you had more than one Null in one or more of your lists? What if I only used 2 _? What if I only used 1 _? Try this. Learn. Go.
– Bill
Commented Oct 30, 2014 at 3:30
• I see. The reason I ask is that the arrays I'm using a much larger and some rows didn't get "Null" removed. I'll do some experimenting. Thanks for the help Commented Oct 30, 2014 at 5:00
• Compare a few of the rows that didn't get Null removed with those that did. See if you notice anything about the first group that differs from the second group. I have a hunch, but without data can't be sure. What I showed was one way that worked with your example. Sometimes after getting an answer people might realize that wasn't really the question they should have asked. You might find some of your rows differ from your example and need a different answer. Note that with Mathematica there are always a dozen different ways of doing anything, at least a couple of which are incomprehensible.
– Bill
Commented Oct 30, 2014 at 5:05