5
$\begingroup$

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.

$\endgroup$
2
  • 3
    $\begingroup$ Look into Position[]. $\endgroup$ Commented May 5, 2012 at 10:20
  • 7
    $\begingroup$ 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. $\endgroup$ Commented May 5, 2012 at 10:41

2 Answers 2

12
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ @Harald you are welcome :) $\endgroup$ Commented May 5, 2012 at 10:40
  • $\begingroup$ is there way to find position on the 1st level, but with criterion pointing on 2nd level 2nd position? Eg. To return position of 1st level list that contains 3 on the 2nd position in it, then return 2 as a result. $\endgroup$ Commented Jul 4, 2016 at 10:34
11
$\begingroup$

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}
$\endgroup$
0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.