Wolfram Computation Meets Knowledge

SelectIndices

Contributed by: Taliesin Beynon

Find indices of parts satisfying criterion

ResourceFunction["SelectIndices"][list,crit]

gives indices of elements ei of list for which crit[ei] yields True.

ResourceFunction["SelectIndices"][crit]

is the operator form of ResourceFunction["SelectIndices"].

Details and Options

The object list can have any head, not necessarily List.
When used on an ,ResourceFunction["SelectIndices"] picks out keys.
ResourceFunction["SelectIndices"] can be used on SparseArray objects.
ResourceFunction["SelectIndices"][crit][list] is equivalent to ResourceFunction["SelectIndices"][crit][list].

Examples

Basic Examples

Select indices of even elements:

In[1]:=
ResourceFunction["SelectIndices"][{1, 2, 4, 7, 6, 2}, EvenQ]
Out[1]=

The operator form of SelectIndices:

In[2]:=
ResourceFunction["SelectIndices"][EvenQ][{1, 2, 4, 7, 6, 2}]
Out[2]=

Scope

Select indices of a list:

In[3]:=
ResourceFunction["SelectIndices"][{1, 2, 3, 4}, EvenQ]
Out[3]=

Select indices of an expression with an arbitrary head:

In[4]:=
ResourceFunction["SelectIndices"][f[1, 2, 3, 4], EvenQ]
Out[4]=

Keys of an Association:

In[5]:=
ResourceFunction[
 "SelectIndices"][<|a -> 1, b -> 2, c -> 3, d -> 4|>, # > 2 &]
Out[5]=

Indices of a SparseArray:

In[6]:=
s = SparseArray[Table[2^i -> i, {i, 0, 5}]]
Out[6]=
In[7]:=
ResourceFunction["SelectIndices"][s, OddQ]
Out[7]=

Use SelectIndices in operator form:

In[8]:=
ResourceFunction["SelectIndices"][EvenQ][Range[5]]
Out[8]=

Resource History