# How can I avoid using a For-loop in my code?

I am trying to avoid using For because I heard that For is not cool in functional programming. Below is a problem that the first idea came to my mind is using For. How can I change this into a code using Table or Range or something else?

What the code does is to extract the elements in a list ls which satisfies certain condition into a new list lsNew.

ls = RandomInteger[20, 1000];
lsNew = {};
For[i = 1, i <= Length[ls], i++,
If[ls[[i]] < 6, lsNew = Append[lsNew, ls[[i]]]]]
lsNew

• Use Select instead. The setup is much, much simpler, and it doesn't use $O(n^2)$ methods to do it. (Append used like this is the culprit.) Dec 29, 2015 at 3:38
• Dec 29, 2015 at 3:41

Have a look at Select.

lsNew = Select[# < 6 &]@ls

• As from 10.3 we can also use LessThan[6] instead of the pure function. Dec 29, 2015 at 12:58
• @SimonWoods Ah, the operator forms of inequalities. Thanks for the heads up. Dec 29, 2015 at 13:18

OK, the related post I linked above may be too long, so let me extract the relevant part:

Pick[#, UnitStep[# - 6], 0] &@ls


Just for fun, here's a somewhat strange solution:

ls /. $_ /;$ >= 6 :> (## &[])


Although Select is the classical Mathematica function for doing what you ask, in V10.2 or later one can map an If expression (for some a more natural way to express the problem) and get the desired results.

SeedRandom[42]; data = RandomInteger[20, 100];
If[# < 6, #, Nothing] & /@ data


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

This is likely to run slower the Select, but will run much faster than a For-loop.

• One can still use If to solve the problem before v10.2, by using these instead of Nothing :). Dec 29, 2015 at 5:15
• I find: " Select items < 6 ", to be a fairly natural way to express the problem. Dec 29, 2015 at 17:42
• @Edmund. I do too. But others don't. Note that I qualified my statement with "some". Dec 29, 2015 at 23:16