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:

If I have a vector {1,2,3}, I want to write an If statement, where if one of the positions is "1", it will perform a certain command.

test = {1, 2, 3}
# == 1 & /@ test 
If[# == 1 & /@ test == True, Print["Yes"], Print["no"]]

I get this as the output:

If[{True, False, False} == True, Print["Yes"], Print["no"]]

What am I doing wrong? I am new to Mathematica.

share|improve this question

marked as duplicate by Kuba, Yves Klett, m_goldberg, RunnyKine, Sjoerd C. de Vries Apr 29 '14 at 21:43

This question was marked as an exact duplicate of an existing question.

Use MemberQ to test if an element is in the list. – Szabolcs Apr 29 '14 at 18:31
You should study the answers to this question Map a function across a list conditionally. In fact this is a duplicate. – Artes Apr 29 '14 at 18:34

There are a number of different solutions. Here are a few:

test = {1, 2, 3}
# == 1 & /@ test

If[Or @@ Thread[# == 1 & /@ test == True], Print["Yes"], Print["no"]]
If[Or @@ (# == 1 & /@ test), Print["Yes"], Print["no"]]
If[MemberQ[test, 1], Print["Yes"], Print["No"]]

In my opinion these are in increasing order of clarity and good practice.

share|improve this answer

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