Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

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?

share|improve this question

marked as duplicate by Mr.Wizard Feb 19 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.

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

1 Answer 1

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 at 20:59

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