I have code which inserts an expression into lists that I need to remove. I can insert anything I want, a string, variable, or number, into the lists. Suppose I insert the number 13 as my blacklisted element and have a list like
l={12, 14 y, 13, 13 x, 13 x -y}
I then implement
Select[l, # != 13 &]
but the output is
12
My desired output in this case would have been
{12, 14 y, 13 x, 13 x -y}
I've created a working solution that doesn't feel as elegant as it could be, with now a string blacklist element "13"
Select[{12, 14 y, "13", 13 x, 13 x - y}, Not@StringMatchQ[ToString[#], "13"] &]
which gives the desired output. Is it possible to refine the original attempt at a solution to work a bit more simply?
Select[l, !MatchQ[#, 13] &] ]is what you're looking for... Your own approach can be modified by usingUnsameQor=!=instead of!=which won't evaluate. – rm -rf Feb 19 '14 at 18:26Select[l, # =!= 13 &]works. – andre314 Feb 19 '14 at 18:30==can remain unevaluated while===will always evaluate. – rm -rf Feb 19 '14 at 18:32Cases[l, Except[13]](or "13" is you make it a string) – Mike Honeychurch Feb 19 '14 at 20:14DeleteCasesis "easily found in the documentation" leaving the nontrivial aspect the behavior ofUnequal, which is largely covered in R.M's referenced answer. I shall also add links to some related questions. – Mr.Wizard Feb 19 '14 at 21:09