Skip to content

Position ​

Every position value occurs at in the collection.

✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram LanguagePosition✓
Position(collection, value)every position value occurs at, each wrapped in its own ListList.
Typed signatures
TypePackageApplies
(indexed_collection<T>, predicate: (T) any -> boolean) -> list<integer> where T: anycompute-enginealways
(indexed_collection<any>, any) -> list<integer> | list<list<integer>>combinatoricsreplaces compute-enginealways

Domain: Collections

Details
  • Wolfram's answer where IndexOf reports only the first occurrence, as a plain index.
  • An empty ListList when the value isn't present.
  • Each position is a ListList of indices, one per level: the search goes into nested collections, so {2,3}\{2, 3\} is the third part of the second.
  • Heads count as well, at index 00: Position({{1}, {2}}, List) is {{0}, {1, 0}, {2, 0}}.

Examples

Basic #
live ↗
InPosition([1,2,3,2],2)\mathrm{Position}(\bigl\lbrack1, 2, 3, 2\bigr\rbrack, 2)Out[[2],[4]]\bigl\lbrack\bigl\lbrack2\bigr\rbrack, \bigl\lbrack4\bigr\rbrack\bigr\rbrack

A value absent from the collection has no positions

live ↗
InPosition([1,2,3],9)\mathrm{Position}(\bigl\lbrack1, 2, 3\bigr\rbrack, 9)Out[]\bigl\lbrack\bigr\rbrack

Every occurrence, not just the first, each as a position list

live ↗
InPosition([a,b,a,a,b,c,b],b)\mathrm{Position}(\bigl\lbrack a, b, a, a, b, c, b\bigr\rbrack, b)Out[[2],[5],[7]]\bigl\lbrack\bigl\lbrack2\bigr\rbrack, \bigl\lbrack5\bigr\rbrack, \bigl\lbrack7\bigr\rbrack\bigr\rbrack
live ↗
InPosition([[a,a,b],[b,a,a],[a,b,a]],b)\mathrm{Position}(\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)Out[[1,3],[2,1],[3,2]]\bigl\lbrack\bigl\lbrack1, 3\bigr\rbrack, \bigl\lbrack2, 1\bigr\rbrack, \bigl\lbrack3, 2\bigr\rbrack\bigr\rbrack
Properties #

Position's first entry agrees with IndexOf

live ↗
InPosition([1,2,3,2],2)[1]\mathrm{Position}(\bigl\lbrack1, 2, 3, 2\bigr\rbrack, 2)[1]Out[2]\bigl\lbrack2\bigr\rbrack
About
Examples5
Checked againstWolfram Language 5 agree
CrosswalkWolfram Language

See also: IndexOf, At