Part

or Part[expr,i]
gives the i^(th) part of expr.


counts from the end.

or Part[expr,i,j,]
is equivalent to .


gives a list of the parts , , of expr.


gives parts m through n.


gives parts m through n in steps of s.


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

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

DetailsDetails

  • You can make an assignment like 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, 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 . Each part has one index from each list.
  • If any of the 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, gives the parts in the corresponding ordinary array. »
  • The form 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.
  • and Key[k] can appear anywhere in the specification of parts.
  • In StandardForm and InputForm, can be input as .
  • and can be entered as Esc[[Esc and Esc]]Esc or \[LeftDoubleBracket] and \[RightDoubleBracket].
  • In StandardForm, can be input as or .

Background
Background

  • 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 or . 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. .
  • 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 and Rational[1,2] is the internal representation for numbers such as . Functions having this property are said to be atomic, and they return True when AtomQ is applied to them.

ExamplesExamplesopen allclose all

Basic Examples  (15)Basic Examples  (15)

Pick out a part of a list:

In[1]:=
Click for copyable input
Out[1]=

Pick out a sequence of parts:

In[1]:=
Click for copyable input
Out[1]=

Pick out a part of a matrix:

In[1]:=
Click for copyable input
Out[1]=

The second row of a matrix:

In[1]:=
Click for copyable input
Out[1]=

The second column of a matrix:

In[1]:=
Click for copyable input
Out[1]=

An element 2 from the end of a list:

In[1]:=
Click for copyable input
Out[1]=

A list of parts:

In[1]:=
Click for copyable input
Out[1]=

A part of an Association corresponding to key :

In[1]:=
Click for copyable input
Out[1]=

The first part of an Association:

In[1]:=
Click for copyable input
Out[1]=

The part associated with the key :

In[2]:=
Click for copyable input
Out[2]=

Take several parts in an Association:

In[1]:=
Click for copyable input
Out[1]=

Extract parts by keys:

In[1]:=
Click for copyable input
Out[1]=

Extract subexpressions from an Association:

In[1]:=
Click for copyable input
Out[1]=

Reassign a part:

In[1]:=
Click for copyable input
Out[1]=
In[2]:=
Click for copyable input
Out[2]=
In[3]:=
Click for copyable input
Out[3]=

Reassign a sequence of parts:

In[1]:=
Click for copyable input
Out[1]=
In[2]:=
Click for copyable input
Out[2]=
In[3]:=
Click for copyable input
Out[3]=

Part works with expressions of any kind, not just lists:

In[1]:=
Click for copyable input
Out[1]=
In[2]:=
Click for copyable input
Out[2]=
In[3]:=
Click for copyable input
Out[3]=

Part always operates on the FullForm of expressions:

In[4]:=
Click for copyable input
Out[4]=
In[5]:=
Click for copyable input
Out[5]//FullForm=
Introduced in 1988
(1.0)
| Updated in 2014
(10.0)