Message Boards Message Boards


Delete elements containing numeric entries from a list?

Posted 2 months ago
4 Replies
0 Total Likes

Hi, I have a large list of 1*2 lists that contain various dummy variables, something like

list = {{a, b + c}, {u, y - h}, {d, g + h}, {d, 0}, {v, 0}}

I want to remove every element that has zero as the second entry, I have tried Select, DeleteCases andCases but none worked because Mathematica tried to equate variables to zero

How can I do that?



4 Replies

I would try DeleteCases[list, {_, 0}].

I would try DeleteCases[list, {_, 0}].

Posted 2 months ago

It worked! Thanks.

My original post got corrupted and it repeated Gianluca's post.

I added to Gianluca's answer that there are many ways to do this and that your original attempts may have worked if you used === (SameQ) instead of ==. Mathematica does not know if g+h == 0 because g and h are not yet defined. However, Mathematica does know that g+h are not identical to 0. So if you are testing in Select, DeleteCases, Cases, etc. you usually need to use SameQ or UnSameQ and not Equals



Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract