Function Repository Resource:

SelectIndices

Source Notebook

Find indices of parts satisfying a criterion

Contributed by: Taliesin Beynon

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 Association, ResourceFunction["SelectIndices"] picks out keys.
ResourceFunction["SelectIndices"] can be used on SparseArray objects.
ResourceFunction["SelectIndices"][crit][list] is equivalent to ResourceFunction["SelectIndices"][list,crit].

Examples

Basic Examples (2) 

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 (5) 

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]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 16 January 2019

Related Resources

License Information