Message Boards Message Boards

Delete elements containing numeric entries from a list?

GROUPS:

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?

Thanks

eftsrd

POSTED BY: eft rsd
Answer
10 days ago

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

POSTED BY: Gianluca Gorni
Answer
10 days ago

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

POSTED BY: Neil Singer
Answer
10 days ago

It worked! Thanks.

POSTED BY: eft rsd
Answer
9 days ago

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

Regards.

Neil

POSTED BY: Neil Singer
Answer
9 days ago

Group Abstract Group Abstract