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.

Can the Element[] function only be used for the domains given in the documentation? I am trying to use it to determine if a value is contained in a previously defined list. Thanks!

share|improve this question
    
It only works for domains, yes. You might be interested in MemberQ[]... –  J. M. May 28 '13 at 14:00
    
Thanks. That should do the trick –  yankeefan11 May 28 '13 at 14:03
    
You should be able to answer your own question, now... –  J. M. May 28 '13 at 14:04
2  
There is an exception to J.M.'s answer: upvalues, although it would only be shorthand for MemberQ[]. –  rcollyer May 28 '13 at 14:06
2  
Difference is also that Element is a kind of assertion, you tell Mathematica that a given variable belongs to a given domain, whereas MemberQ is a test; you ask Mathematica whether a value is member of a list. –  Sjoerd C. de Vries May 28 '13 at 20:26
add comment

2 Answers

There are as usual several ways. The most convenient one is probably MemberQ, as already pointed out in the comments.

MemberQ[{1, 2, 3, 4, 9}, 9]
(* True *)

If you have a complex structure and not only a flat list, then FreeQ can be of use. Note that you have to negate the result.

MemberQ[{1, 2, {3}, {{4, 9}}}, 9]
Not[FreeQ[{1, 2, {3}, {{4, 9}}}, 9]]
(* False *)
(* True *)

Finally, note that you can specify the level where MemberQ should look for the appearance of the element

MemberQ[{1, 2, {3}, {{4, 9}}}, 9, Infinity]
(* True *)
share|improve this answer
add comment

I have not tested it, but this dirty hack might be pretty fast IF you have to assess membership in a list of symbolic values with no zeros.

symMemberQ[x_, mylist_] := SameQ[Times @@ Flatten[mylist /. x -> 0], 0]

Adding Flatten wil take care of nested list.

symMemberQ[e, {a, {b, g}, {d, {e, f}}}]
   True
symMemberQ[w, {a, {b, g}, {d, {e, f}}}]
   False

(It's a dirty hack, so beware: it might bite you).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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