### Basic Examples

Basic Examples

Add indices to a list of elements:

[{a,b,c,d}]

In[]:=

{{1,a},{2,b},{3,c},{4,d}}

Out[]=

### Scope

Scope

Start with the index 4:

[{a,b,c,d},4]

In[]:=

{{4,a},{5,b},{6,c},{7,d}}

Out[]=

The starting index and the step can be symbolic. Start the index at x:

[{a,b,c,d},x]

In[]:=

{{x,a},{1+x,b},{2+x,c},{3+x,d}}

Out[]=

The step can also be symbolic:

[{a,b,c,d},x,y]

In[]:=

{{x,a},{x+y,b},{x+2y,c},{x+3y,d}}

Out[]=

### Applications

Applications

Select palindromic words from a while retaining their indices:

s={"anna","has","a","red","racecar",",","wow","!"};s=

[s];s=Select[s,Last/*PalindromeQ]

In[]:=

{{1,anna},{3,a},{5,racecar},{6,,},{7,wow},{8,!}}

Out[]=

Sort the strings and retain their original indices:

s={"anna","has","a","red","racecar",",","wow","!"};s=

[s];SortBy[s,Last]

In[]:=

{{8,!},{6,,},{3,a},{1,anna},{2,has},{5,racecar},{4,red},{7,wow}}

Out[]=

### Properties and Relations

Properties and Relations

AddIndices

MapIndexed[{#2[[1]],#1}&,{a,b,c,d,e}]===

[{a,b,c,d,e}]

In[]:=

True

Out[]=

AddIndices

ele=RandomReal[{0,1},10^5];First[RepeatedTiming[MapIndexed[{#2[[1]],#1}&,ele];]],FirstRepeatedTiming

[ele];

In[]:=

{0.0948,0.026}

Out[]=