Skip to content

IndexOf ​

The position of the first occurrence of value in the collection.

IndexOf(collection, value)the position of the first occurrence of value.
Typed signatures
TypePackageApplies
(indexed_collection<any>, any) -> integercompute-enginealways

Domain: Collections

Details
  • Returns 0 when the value isn't present, since 0 is never a valid position.
  • Round-trips with At: At(c,IndexOf(c,v))=vAt(c, IndexOf(c, v)) = v whenever v occurs in c.
  • Reports only the first occurrence, as a plain index. See Position for every occurrence.

Examples

Basic #
live ↗
InIndexOf([1,2,3],2)\mathrm{IndexOf}(\bigl\lbrack1, 2, 3\bigr\rbrack, 2)Out22

0 signals that the value isn't present, since 0 is never a valid position

live ↗
InIndexOf([1,2,3],9)\mathrm{IndexOf}(\bigl\lbrack1, 2, 3\bigr\rbrack, 9)Out00

Only the first of several occurrences is reported

live ↗
InIndexOf([a,b,a,a,b,c,b],b)\mathrm{IndexOf}(\bigl\lbrack a, b, a, a, b, c, b\bigr\rbrack, b)Out22
Scope #

The value can itself be a list

live ↗
InIndexOf([[1,2],[3,4]],[3,4])\mathrm{IndexOf}(\bigl\lbrack\bigl\lbrack1, 2\bigr\rbrack, \bigl\lbrack3, 4\bigr\rbrack\bigr\rbrack, \bigl\lbrack3, 4\bigr\rbrack)Out22
Properties #

At(c,IndexOf(c,v))=vAt(c, IndexOf(c, v)) = v whenever v occurs in c. See At

live ↗
In[10,20,30][IndexOf([10,20,30],20)]=20\bigl\lbrack10, 20, 30\bigr\rbrack[\mathrm{IndexOf}(\bigl\lbrack10, 20, 30\bigr\rbrack, 20)]=20OutTrue\mathrm{True}
Possible issues #

IndexOf reports only the first occurrence; see Position for every one

live ↗
InIndexOf([1,2,3,2],2)\mathrm{IndexOf}(\bigl\lbrack1, 2, 3, 2\bigr\rbrack, 2)Out22

IndexOf stays at the top level, where this value doesn't occur at all

live ↗
InIndexOf([[a,a,b],[b,a,a],[a,b,a]],b)\mathrm{IndexOf}(\bigl\lbrack\bigl\lbrack a, a, b\bigr\rbrack, \bigl\lbrack b, a, a\bigr\rbrack, \bigl\lbrack a, b, a\bigr\rbrack\bigr\rbrack, b)Out00
About
Examples7
Checked againstWolfram Language 7 agree

See also: At, Count, Position, FirstPosition