# Removing all values from a list that satisfy a given condition

Suppose I have a list,

list = Table[{n}, {n, 1, 5}]
(* list = {{1},{2},{3},{4},{5}} *)


and I want to remove all values from this list that are greater than 3, so that I get

newlist = {{1}, {2}, {3}}


Is there a way to do this without having something cumbersome like

newlist1 = list /. {4} -> Sequence[]
(* newlist1 = {{1}, {2}, {3}, {5}} *)
newlist = newlist1 /. {5} -> Sequence[]
(* newlist = {{1}, {2}, {3}} *)


possibly with an If statement?

Some context: I have a set of circles and a list of each circle's radius, but I want to remove all the circles with radius $r$ outside of a few set ranges. For example, if I have a hundred circles with radii ranging from 1 to 10, I would then like to eliminate all the circles with radii $r<2$, those with $4<r<6$, and those with $r>8$.

Any suggestions are appreciated.

Edit: Here's what I've tried by adapting Nasser's code:

rad = {34.6302, 10.0623, 6.94622, 26.3059, 52.6308,
27.1662, ... , 80.0562, 799.3, 44.5997, 14.0357}

DeleteCases[rad, {x_} /; {x>10 And x<700}]
(* the 799.3 is an extreme outlier *)


but I apparently don't understand the syntax required for setting multiple constraints. I've tried different variations, like

DeleteCases[rad, {x_,y_} /; {x>3, y<1}]


but again, no luck.

• DeleteCases[list, x_ /; First@x > 3] If you had made your table like this: Table[n, {n, 1, 5}] it would be easier a little: DeleteCases[list, x_ /; x > 3] Nov 6, 2014 at 3:33
• Nice, that works perfectly for the really simple example I posted. How can I modify it for the circle example? And yeah, I had defined list as I did because /. 4->Sequence[] didn't work properly. Nov 6, 2014 at 3:38
• Please include minimal code for your "circles" example. Nov 6, 2014 at 3:49
• In your new code, replace it with this: DeleteCases[rad, x_ /; (x > 10 && x < 700)] then it will work. I get {6.94622, 799.3} Nov 6, 2014 at 3:58
• If your list has {{a},{b}....} and not {a,b,c}, then use DeleteCases[rad, x_ /; (First@x > 10 && First@x < 700)] You need First@ to be do x[] basically, since each x is a list now. (you can write x[] if you prefer. Nov 6, 2014 at 4:08

rad = {34.6302, 10.0623, 6.94622, 26.3059, 52.6308, 27.1662, 80.0562, 799.3, 44.5997, 14.0357, 1000};

DeleteCases[rad, x_ /; 10 > x || x > 700]

(* {34.6302, 10.0623, 26.3059, 52.6308, 27.1662, 80.0562, 44.5997, 14.0357} *)


You need an urgent syntax surgery. (* lol *)

Or using the converse, Cases:

Cases[rad, x_ /; 10 <= x <= 700]

• Yep, still fairly new to Mathematica. Thanks for the assist. Nov 7, 2014 at 5:56

Here's my contribution, making the same assumptions as the other two answers:

rad = {34.6302, 10.0623, 6.94622, 26.3059, 52.6308, 27.1662, 80.0562, 799.3, 44.5997,
14.0357, 1000};


{34.6302, 10.0623, 26.3059, 52.6308, 27.1662, 80.0562, 44.5997, 14.0357}

list = Table[{n}, {n, 1, 5}];

Select[list, #[] <= 3 &]
(*{{1}, {2}, {3}}*)

rad = {34.6302, 10.0623, 6.94622, 26.3059, 52.6308, 27.1662, 80.0562, 799.3, 44.5997, 14.0357, 1000};


Just for fun (noting would need to vary depending on strictness of inequalities):

Clip[rad, {10, 700}, {"out", "out"}] /. ("out" :> Sequence[])
Flatten @ Clip[rad, {10, 700}, {{}, {}}] (* as per Mr. Wizard*)
DeleteCases[Clip[rad, {10, 700}, {"out", "out"}], "out"]
Pick[rad, UnitStep[# - 10] - UnitStep[# - 700] & @ rad, 1]
(* latter: improvement as per Mr. Wizard , see comment*)


all yield:

(*{34.6302, 10.0623, 26.3059, 52.6308, 27.1662, 80.0562, 44.5997, \
14.0357}*)

• Another one for your Clip set: Flatten @ Clip[rad, {10, 700}, {{}, {}}] Nov 6, 2014 at 9:41
• I just noticed that you're mapping UnitStep. You're doing it wrong (case #4). ;^) Replace /@ with @ for a nice performance improvement. Nov 6, 2014 at 9:54
• @Mr.Wizard thank you...I too often forget which functions are listable. Nov 6, 2014 at 10:01