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.

expr[["key"]]

gives the value associated with the key "key" in an association expr.

expr[[Key[k]]]

gives the value associated with an arbitrary key k in the association expr.

Details

  • 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.
  • Notice that lists are used differently in Part than in functions like Extract, MapAt, and Position.
  • If expr is a SparseArray object, expr[[]] gives the parts in the corresponding ordinary array. »
  • The form expr[["key"]] can be used to extract a value from an association whose key is a string. expr[[Key[k]]] can be used to extract values with any keys.
  • "key" and Key[k] can appear anywhere in the specification of parts.
  • In StandardForm and InputForm, expr[[spec]] can be input as exprspec.
  • and can be entered as [[ and ]] or \[LeftDoubleBracket] and \[RightDoubleBracket].
  • In StandardForm, expr[[spec]] can be input as expr[[spec]] or exprspec.

Background & Context

  • Part is a structural function that gives a specified indexed part of an expression. The expression Part[expr,i] is commonly represented using the shorthand syntax expr[[i]] or expri. Part can be used to pick out parts of lists, a sequence of parts, elements of matrices, rows and columns of matrices, and so forth. Part can also be used to assign values to parts using Set, e.g. list[[k]]=newValue.
  • Although Part is most commonly used with lists, it works with expressions of any kind. When using Part, parts of expressions may be specified using indices, lists of indices, Span expressions, or All.
  • Useful functions that perform common special cases of Part include First, Last, Take, Drop, Rest, and Most. Position can be used to find the positions in an expression at which specified content appears. Extract is a more specialized function that extracts the part of an expression at the position specified by a given list, while the analogous function Delete removes an element from a specified position.
  • A small number of functions contain parts that cannot be accessed or divided into subexpressions using Part or related functions. The most common cases are Complex and Rational, where for example Complex[1,2] is the internal representation for numbers such as 1+2 and Rational[1,2] is the internal representation for numbers such as 1/2. Functions having this property are said to be atomic, and they return True when AtomQ is applied to them.

Examples

open allclose all

Basic Examples  (15)

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:

A part of an Association corresponding to key "a":

The first part of an Association:

The part associated with the key 1:

Take several parts in an Association:

Extract parts by keys:

Extract subexpressions from an Association:

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:

Scope  (10)

Equivalent forms:

Pick out parts 1 and 3:

Pick out parts 2 and 3 of parts 1 and 3:

Parts 1 through third-to-last:

Shorter notation:

Parts 3 through third-to-last extracted in steps of 2:

Parts extracted in steps of 2 starting at the beginning:

Enter in FullForm:

Enter using [[ and ]]:

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:

Generalizations & Extensions  (3)

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:

Applications  (4)

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:

Properties & Relations  (2)

Pick out parts cyclically by using Mod with offset 1:

Take picks out ranges of parts:

Possible Issues  (6)

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:

If an expression contains held parts, extraction may cause them to evaluate:

This can lead to a difference between direct part extraction and successive part extraction:

Only parts that already exist can be reassigned:

Create a shared variable whose value is a large matrix:

Accessing its elements causes the whole matrix to be transferred to the subkernels repeatedly:

Use Unevaluated to allow the special code for parts of shared variables to see the variable:

Introduced in 1988
 (1.0)
 |
Updated in 1996
 (3.0)
1999
 (4.0)
2000
 (4.1)
2002
 (4.2)
2003
 (5.0)
2007
 (6.0)
2014
 (10.0)