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.

Is it possible to easily find the position of a specific value in a list? For example, if the list is {{x1,y1},{x2,y2},...,{xn,yn}} I would like to know the position of y = 123456.78. I would prefer to not use If.

share|improve this question
2  
Look into Position[]. –  J. M. May 5 '12 at 10:20
    
oh, thanks. I didn't know this option... –  Harald May 5 '12 at 10:23
6  
There is a subtlety for using Position, which is good to know. On an unrelated matter: why don't you accept those answers to your previous questions which you like or prefer. –  Leonid Shifrin May 5 '12 at 10:41

2 Answers 2

up vote 8 down vote accepted

You can use the Position function. Which returns a list of the positions of the pattern you are searching for.

k = RandomInteger[10, {10, 2}]
(* ->{{2, 10}, {9, 3}, {9, 10}, {8, 1}, {2, 0}, {10, 10}, {10, 0}, {10,6}, {3, 1}, {3, 2}} *)

Position[k, 3]

returns

{{2, 2}, {9, 1}, {10, 1}}

Which is a list of the indices of the three occurrences of 3 in the list, k.

share|improve this answer
    
thanks a lot for your help, Harald –  Harald May 5 '12 at 10:33
    
@Harald you are welcome :) –  image_doctor May 5 '12 at 10:40

Position[] is the function you want:

la = RandomInteger[{-9, 9}, {8, 2}]
{{-4, -4}, {1, -8}, {3, 0}, {-2, -3}, {-3, -1}, {4, 9}, {2, 4}, {0, -2}}

Position[la, 0]
{{3, 2}, {8, 1}}

(* check results *)
la[[##]] & @@@ %
{0, 0}
share|improve this answer
    
Thanks a lot, Harald –  Harald May 5 '12 at 10:33

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.