# How to use Select to exclude a single element which I can control? [duplicate]

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 using UnsameQ or =!= instead of != which won't evaluate.
– rm -rf
Feb 19, 2014 at 18:26
• Select[l, # =!= 13 &] works. Feb 19, 2014 at 18:30
• I'm sure this is a duplicate, but the closest I can find is Evaluating an If condition to yield True/False which explains that == can remain unevaluated while === will always evaluate.
– rm -rf
Feb 19, 2014 at 18:32
• Thanks, man =!= instead of != (which surprisingly has an <esc ! = esc> nice not-equal look to it). It's the little things sometimes. Feb 19, 2014 at 18:34
• @Steve Yes it is Feb 19, 2014 at 18:40

More straigthforward than looking for elements that do not match criteria would be to delete those that match them:

l = {12, 14 y, 13, 13 x, 13 x -y} ;

DeleteCases[l, 13]
DeleteCases[l, 13 | 14 y]

{12, 14 y, 13 x, 12 x - y}

{12, 13 x, 12 x - y}


Am I correct or have I missed the point again? ;P

• forests and trees! this is by far the most obvious way to do it Feb 19, 2014 at 20:59