Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

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


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?

share|improve this question

marked as duplicate by Mr.Wizard Feb 19 '14 at 21:09

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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. – R. M. Feb 19 '14 at 18:26
Select[l, # =!= 13 &] works. – andre Feb 19 '14 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. – R. M. Feb 19 '14 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. – Steve Feb 19 '14 at 18:34
@Steve Yes it is – andre Feb 19 '14 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

share|improve this answer
forests and trees! this is by far the most obvious way to do it – Mike Honeychurch Feb 19 '14 at 20:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.