2
$\begingroup$

I have two lists:

list = {1, 2, 3, 4, 5}
slist = {3, 5}

I want to find the positions of the elements in slist in list. I tried it like this and I dont understand why this is not working:

Position[list,slist]

Instead I have to use:

Position[list, slist[[1]]]

and

Position[list, slist[[2]]]

but this seems tedious and there must be an easier way, which I'm unable to find at the moment :)

$\endgroup$

marked as duplicate by Kuba, Bob Hanlon, dr.blochwave, m_goldberg, MarcoB Jul 8 '15 at 15:08

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.

  • $\begingroup$ {3, 5} is not a pattern that is present at any position within {1, 2, 3, 4, 5}. $\endgroup$ – Karsten 7. Jul 8 '15 at 11:42
2
$\begingroup$

Or just do a simple mapping:

Position[list,#]& /@ slist

Should be your Mathematica bread and butter anyways.

$\endgroup$
7
$\begingroup$
Position[list, Alternatives @@ slist]

{{3}, {5}}

$\endgroup$
1
$\begingroup$

Try the following

    Flatten[Table[Position[list, _?(# == slist[[i]] &)], {i, 1, Length[slist]}]]
$\endgroup$

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