FindI

FindI

FindI[ expr, list ] returns the position of the first occurrence of an expression in a list.

Usages

FindI[ expr, list ]

FindI[ expr, list ] returns the position (starting at 1) of the first element of list that is equal to expr. If expr does not occur in list, the result is -1.

Example in out
c is at position 3. FindI[ c, {a,b,c,d} ] 3
Only the first occurrence counts. FindI[ c, {a,b,c,d,c} ] 3
c does not occur in the list. FindI[ c, {a,b,a,d} ] -1
Script variables. x := 7
l := {2,3,5,7,11}
r := FindI[x, l]
r = 4

FindI[ {expr1, expr2, ...}, list ]

If the first argument is a list, FindI searches for each of its elements and returns a list of positions, with -1 for each element that is not found.

Example in out
The positions of a and c. FindI[ {a,c}, {a,b,c,d} ] {1, 3}
3 does not occur in the list. FindI[ {c,3}, {a,b,c,d} ] {3, -1}
A list with one element also gives a list as the result. FindI[ {c}, {a,b,c,d} ] {3}
Details
  • Equal elements: elements are equal if they are the same expression, regardless of the order of terms or factors: FindI[ 1+x, {2, x+1} ] returns 2. Numbers must have the same form: FindI[ 12/5, {2.4, 3} ] returns -1.
  • Empty list: FindI[ c, {} ] returns -1.
Invalid input Result
FindI[ c, a ] Left unevaluated: the result is FindI[ c, a ] itself. The second argument must be a list.
Warning

To search for a list inside a list, put it inside another list. FindI[ {1,2}, { {1,2}, 3} ] searches for 1 and 2 separately and returns {-1, -1}, while FindI[ { {1,2} }, { {1,2}, 3} ] returns {1}.

See also: IntersectI, MaxI, MinI, RemoveI