Mathematica 9 is now available
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.
Mathematica > Core Language > Expressions > Parts of Expressions >

Part ([[...]])

expr[[i]] or Part[expr, i]
gives the i^(th) part of expr.
expr[[-i]]
counts from the end.
expr[[i, j, ...]] or Part[expr, i, j, ...]
is equivalent to expr[[i]][[j]]....
expr[[{i1, i2, ...}]]
gives a list of the parts i1, i2, ... of expr.
expr[[m;;n]]
gives parts m through n.
expr[[m;;n;;s]]
gives parts m through n in steps of s.
  • You can make an assignment like t[[spec]]=value to modify any part or sequence of parts in an expression.  »
  • Part 0 of an expression is its head.
  • Common spans of parts include:
expr[[m;;]]part m through the end
expr[[;;n]]from the beginning to part n
expr[[;;,j]]column j
expr[[m1;;n1,m2;;n2]]submatrix
  • When expr is a list, expr[[{i1, i2, ...}]] gives a list of parts. In general, the head of expr is applied to the list of parts.  »
  • You can get a nested list of parts from expr[[list1, list2, ...]]. Each part has one index from each list.
  • If any of the listi are All or ;;, all parts at that level are kept.
  • If expr is a SparseArray object, expr[[...]] gives the parts in the corresponding ordinary array.  »
  • LeftDoubleBracket and RightDoubleBracket can be entered as Esc [[ Esc and Esc ]] Esc or \[LeftDoubleBracket] and \[RightDoubleBracket].
  • In StandardForm, expr[[spec]] can be input as expr[[spec]] or exprLeftDoubleBracketspecRightDoubleBracket.
Pick out a part of a list:
Pick out a sequence of parts:
Pick out a part of a matrix:
The second row of a matrix:
The second column of a matrix:
An element 2 from the end of a list:
A list of parts:
Reassign a part:
Reassign a sequence of parts:
Part works with expressions of any kind, not just lists:
Part always operates on the FullForm of expressions:
Equivalent forms:
Pick out parts 1 and 3:
Pick out parts 2 and 3 of parts 1 and 3:
Parts 1 through 3 from the end:
Shorter notation:
Parts extracted in steps of 2:
Enter in FullForm:
Enter using Esc [[ Esc and Esc ]] Esc:
Enter as a subscript:
Assign several parts at once:
Assign parts 2 and 3 to be x:
Assign different values to parts 1, 3 and 4:
All standard assignment operations work on parts:
Rearrange elements by reassigning parts:
For SparseArray objects, Part gives the parts in the corresponding ordinary array:
Rows or columns are represented as sparse vectors:
Values can also be set:
The zeroth part of an expression is its head:
Reassign the head:
Heads in the original expression are reused when "lists" of parts are extracted:
Pick out the first solution from an equation:
Pick out all solutions for a univariate equation:
Go through the first 1000 primes, and count how many lie in each possible "mod 10 bin":
Another way to get the same result:
Apply a permutation:
Invert a permutation:
Pick out parts cyclically by using Mod with offset 1:
Take picks out ranges of parts:
Part operates on the FullForm of expressions, not their formatted forms:
Position does not return part specifications in a form that can immediately be used by Part:
Extract extracts parts specified in the way returned by Position:
Successive part extraction is not always equivalent to direct part extraction:
Only parts that already exist can be reassigned:
New in 1 | Last modified in 6
Ask a question about this page  |  Suggest an improvement  |  Leave a message for the team