Part
expr[[i]]
or Part[expr, i]
returns the i
th element of expr
.
Attributes[Part] := {NHoldRest, Protected, ReadProtected}
Simple examples
Return the second item in a list:
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}}
All
to select along the entire dimension:
Further examples
Out of bounds issues will prevent the expression from evaluating:
The input need not be aList
:
Omitting an index will return the original expression: