Skip to content

Part

expr[[i]] or Part[expr, i] returns the ith element of expr.

Attributes[Part] := {NHoldRest, Protected, ReadProtected}

Simple examples

Return the second item in a list:

In[1]:= {a, b, c, d}[[2]]
Out[1]= b
Multi-dimensional indices are supported:
In[2]:= mat = Table[Table[a*b^2, {b, 5}], {a, 5}]
Out[2]= {{1, 4, 9, 16, 25}, {2, 8, 18, 32, 50}, {3, 12, 27, 48, 75}, {4, 16, 36, 64, 100}, {5, 20, 45, 80, 125}}
In[3]:= mat[[5,2]]
Out[3]= 20
Use All to select along the entire dimension:
In[4]:= mat[[5,All]]
Out[4]= {5, 20, 45, 80, 125}

Further examples

Out of bounds issues will prevent the expression from evaluating:

In[1]:= {a}[[2]]
Out[1]= {a}[[2]]
The input need not be a List:
In[2]:= foo[a][[0]]
Out[2]= foo
Omitting an index will return the original expression:
In[3]:= i[[]]
Out[3]= i
In[4]:= {{1, 2}, {3, 4}, {5, 6}}[[All,2]]
Out[4]= {2, 4, 6}