# In a list, how to pick whatever is not f and its derivatives?

Suppose I have a list like:

list1 = {{f[x], Sin[x]}, {f'[x]}, {f''[x]}};
list2 = {{f[x]}, {Cos[x], f''[x]}};


I want to pick these lists and convert them to:

list1 = {{f[x], Framed[Sin[x]]}, {f'[x]}, {f''[x]}};
list2 = {{f[x]}, {Framed[Cos[x]], f''[x]}};


i.e. apply a listable function in these lists that return the list with a function such as Framed[x] applied in whatever functions are not f and its derivatives.

• Very respectfully, but a member for 11 years posting an image instead of properly formatted code is not good for newer members...
– bmf
Feb 25, 2023 at 15:10
• @bmf I was going to post the code, but it gets a kinda unreadable. With lots of [Prime], etc. Feb 25, 2023 at 15:13
• @bmf Done.  Feb 25, 2023 at 15:14
• I had already done it for you. it's the code about the image. but thanks for taking the time to edit :-)
– bmf
Feb 25, 2023 at 15:15
• To make it clear, could you edit your question and show by typing in what the output should be? This will be more clear for the readers I think than trying to just use words to describe the output. I do not know what Framed is in your comment above. Feb 25, 2023 at 15:39

Using Replace:

Replace[#
, k : Except[f[__] | Derivative[_][f][_]] ->
Framed[k
, Background -> Yellow
], {2}
] & /@ {list1, list2} // Column


Now you can parse (read) this roughly as:

Replace from (level 2 only) of the following lists, the pattern k that consists of anything Except f[_] or any of its derivatives and Frame it with options.

• (+1) Very nice, Indeed! Feb 25, 2023 at 18:11
h = Replace[#, b_?(FreeQ @ f) :> Highlighted[b], 2] &;

h @ list1


h @ list2


• How can I enhance it and apply it like: h = Replace[#, b_?(FreeQ[f]) :> Highlighted[b], All]  so that it doesn't highlight the x's? Feb 26, 2023 at 3:09
• I guess I found a way: h = Replace[#, {x -> x, b_?(FreeQ[f]) :> Highlighted[b]}, All] &;. Feb 26, 2023 at 3:11
• The trouble is that it doesn't work if we get more complicated expressions such as list1 = {{f[x], Sin[x]}, {f'[x]}, {f''[x Cos[x]]}}. It will highlight the x Cos[x]. Feb 26, 2023 at 3:17
\$Version

(* "13.2.1 for Mac OS X ARM (64-bit) (January 27, 2023)" *)

ClearAll["Global*"]

list1 = {{f[x], Sin[x]}, {f'[x]}, {f''[x]}, {Sin[x], Cos[x]}};
list2 = {{f[x]}, {Cos[x], f''[x]}};

notf[x_List] := If[FreeQ[#, f], Framed[#], #] & /@ x

notf /@ list1


notf /@ list2


SetAttributes[notf2, Listable];

notf2[x_] := If[FreeQ[x, f], Framed[x], x]

notf2@list1


notf2@list2


• This is really nice, I didn't know FreeQ[]. I guess this will help me A LOT in future projects! Feb 25, 2023 at 16:08

Another way to do this is as follows:

notf = InternalCopyListStructure[#,
If[SameQ[Variables[Level[Head[#], {-1}]], {f}], #, Framed[#]] /@Flatten@#] &;


Test:

notf@list1


notf@list2


Just to remember an undocumented function! :-)

Another way, thanks to the help I got from how-to-use-position-to-select-based-on-absence-of-a-pattern which is to find positions of entries that do not have the patttern f[_] or the pattern for derivative of any order and then use MapAt to put a frame around the positions found.

list1 = {{f[x], Sin[x]}, {f'[x]}, {f''[x]}, {Sin[x], Cos[x]}};
list2 = {{f[x]}, {Cos[x], f''[x]}};
pattern = (FreeQ[#, Alternatives[f[_], Derivative[_][f][_]]] &);
p1 = Position[list1, _?pattern, {2}, Heads -> False];


And now

MapAt[Framed[#] &, list1, p1]


p2 = Position[list2, _?pattern, {2}, Heads -> False];
MapAt[Framed[#] &, list2, p2]


Try this:

list1 = {{f[x], Sin[x]}, {f'[x]}, {f''[x]}};
list2 = {{f[x]}, {Cos[x], f''[x]}};

list1 /. f[x] -> Nothing /. Derivative[n_][f][x] -> Nothing // Flatten

(* {Sin[x]} *)

list2 /. f[x] -> Nothing /. Derivative[n_][f][x] -> Nothing // Flatten

(*   {Cos[x]}  *)


Have fun!

• I'll need the f's and it's derivatives. I just want to replace whatever is not f and it's derivatives by Framed[something_not_f_and_its_derivatives]. Feb 25, 2023 at 16:00
• @Red Banan Look, that's opposite to what you initially wrote. Feb 25, 2023 at 18:16