# How to select elements from a list by condition?

Considering I have a list:

F = {f1, f2, f3, f4}

I want to perform exponent on the list

F = Power[#, 2]& /@ F

Now I want to do the same but with conditions. I want to perform Power[#, 2] on the elements of F that are not equal to NULL and the result of Power[#, 2] is not null.

Thanks.

UPDATE:

Let's say I have

A = {x, y, z}

SomeAction[t_] := ...

SomeAction[#] &/@ A

Let's say x = NULL

As Harry commented SomeAction[#] &/@ Select[A, !(# === NULL) &] will work only on y and z.

Let's also say that SomeAction[y] = NULL, and in this case I want to take the original y.

So I want to have following A = ...SomeAction[#] &/@ Select[A, !(# === NULL) &]... be equivalent to

A = {y, SomeAction[z]}
• something like list = {1, 2, 3, Null, 4}; MapAt[#^2 &, list, Position[list, x_ /; ! (x === Null), {1}, Heads -> False]] ? Commented Feb 16, 2015 at 11:08
• or list = {1, 2, 3, Null, 4}; #^2 & /@ Select[list, ! (# === Null) &] ? Commented Feb 16, 2015 at 11:11
• Using the Listable attribute it usually much faster than using Map in this way, so you could try F^2 /. Null^2 -> Null. Commented Feb 16, 2015 at 11:12
• @Harry right, something like that. But I would also like to control the resulting value, is it possible? Commented Feb 16, 2015 at 11:17
• Commented Feb 16, 2015 at 11:30

Here is a construct you might like:

ifNot[f_, x_][y_] /; MatchQ[y, x] := y

ifNot[f_, x_][y_] := If[MatchQ[#, x], y, #] & @ f[y]

Example:

ifNot[#^2 &, 4] /@ {1, 2, 3, 4, 5}
{1, 2, 9, 4, 25}

This is designed to work with patterns as well as literals for parameter x.

• Shouldn't the fourth number in the answer to the example be 16? At least this is what I calculated in my head and what my Mathematica (11) gives me Commented Oct 3, 2016 at 11:23
• @user1482714 I think you misunderstand the purpose of this function, which is: if the input is not "4" then square it, else return it unmodified. Commented Oct 3, 2016 at 23:34
• Ohh, sorry, I was doing it with 2 for the x_ argument, as this is what I needed in my application. You are completely right and my comment is wrong Commented Oct 4, 2016 at 13:20
• @user1482714 No problem. We all make mistakes; I seem to make more than my share of them. Please continue to call me out when you believe you have spotted one of my mistakes. Commented Oct 5, 2016 at 0:31

For example, here is a way to do what you want with one single function.

In the following example, instead of Null, the "forbiden" value is 1, and someactionis computing Mod[x, 2] function (which gives the remainder on division of x by 2).

Let's define someaction, Mathematica allows to do this :

someaction[x_List] := someaction /@ DeleteCases[x, 1];

someaction[x_] := Mod[x, 2] // Switch[#, 1, x, _, #] &
(* or also someaction[x_] := Mod[x, 2] /. {1 -> x}; *)

The first definition tells what someaction should do when its argument is a List: it deletes all 1's from the list and map on this new list the more general definition of someaction.
The second (more general) defintion of someaction tells the function to perform a particular computation (here Mod[x,2]) but if 1is the result, it will be converted back to the argument (x).

Test

Let's say my test list is :

mylist={1,2,3,4,5,6};

then

someaction[mylist]

returns as expected

{0, 3, 0, 5, 0}

(for comparison : Mod[#,2]/@mylist returns {1, 0, 1, 0, 1, 0})

Clear[myList];
myList = {{1, 3.128}, {2, 2.459}, {2, 6.287}, {7, 6.02}, {2, 15287}};

myListLength = Length[myList];
For[i = 1, i <= myListLength, i++,
If[myList[[i, 1]] == 2, Print[myList[[i]]]]]