GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • WOLFRAM
  • Products
  • AI AccessUse with Mathematica and Wolfram|One
  • Finance Platform
  • Wolfram|Alpha Notebook Edition
  • Application Server
  • Enterprise Private Cloud
  • Wolfram Engine
  • Wolfram Player
  • Wolfram Cloud App
  • Wolfram Player App
  • Wolfram|Alpha Website
  • Wolfram|Alpha APIs
  • Wolfram AI Ecosystem

This view

  • Original page
  • New address

GeezerVison is showing reference.wolfram.com — Part: Get a specified piece of an expression—Wolfram Documentation

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
Details and Options Details and Options
Background & Context
Examples  
Basic Examples  
Scope  
Single-Level Specifications  
Multilevel Specifications  
Assignments  
Input Forms  
Applications  
Properties & Relations  
Possible Issues  
See Also
Tech Notes
Related Guides
Related Links
History
Cite this Page

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 or a structured array such as QuantityArray or SymmetrizedArray, 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 expr〚spec〛.
  • 〚 and 〛 can be entered as [[ and ]] or \[LeftDoubleBracket] and \[RightDoubleBracket].
  • In StandardForm, expr[[spec]] can be input as expr[[spec]] or expr〚spec〛.

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 expr〚i〛. 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 all close all

Basic Examples  (6)

Pick out a part of a list:

Wolfram Language code: {a, b, c, d, e, f}[[3]]

Obtain the second element from the end of a list:

Wolfram Language code: {a, b, c, d, e, f}[[-2]]

Pick out a sequence of parts:

Wolfram Language code: {a, b, c, d, e, f}[[2 ;; 4]]

Specify the part of an association corresponding to key "a":

Wolfram Language code: <|"a" -> 5, "b" -> 6|>[["a"]]
Wolfram Language code: 5

Reassign a part:

Wolfram Language code: v = {a, b, c}
Wolfram Language code: v[[2]] = d
Wolfram Language code: v

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

Wolfram Language code: (1 + 2 x ^ 2 + y ^ 2)[[2]]

Part always operates on the FullForm of expressions:

Wolfram Language code: (x / y)[[2]]
Wolfram Language code: FullForm[x / y]

Scope  (24)

Single-Level Specifications  (7)

Select several parts, maintaining the head:

Wolfram Language code: f[a, b, c, d, e][[{1, 3, 4}]]

Obtain a list of parts, including repeats:

Wolfram Language code: {a, b, c, d, e, f}[[{1, 3, 1, 2, -1, 6}]]

Parts 1 through third-to-last:

Wolfram Language code: {a, b, c, d, e, f}[[1 ;; -3]]

Shorter notation:

Wolfram Language code: {a, b, c, d, e, f}[[ ;; -3]]

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

Wolfram Language code: {a, b, c, d, e, f, g, h, i, j, k}[[3 ;; -3 ;; 2]]

Parts extracted in steps of 2 starting at the beginning:

Wolfram Language code: {a, b, c, d, e, f, g, h, i, j, k}[[ ;; ;; 2]]

The first part of an Association object:

Wolfram Language code: <|2 -> b, 1 -> a|>[[1]]

The part associated with the key 1:

Wolfram Language code: <|2 -> b, 1 -> a|>[[Key[1]]]

Take consecutive parts in an association:

Wolfram Language code: <|"a" -> 1, "b" -> 2, "c" -> 3, "d" -> 4|>[[1 ;; 2]]

Extract parts corresponding to multiple keys:

Wolfram Language code: <|"a" -> 1, "b" -> 2, "c" -> 3|>[[{Key["a"], Key["c"]}]]

Multilevel Specifications  (7)

Pick out a part of a matrix:

Wolfram Language code: {{a, b, c}, {d, e, f}, {g, h, i}}[[2, 3]]

This can be understood as first extracting the middle row:

Wolfram Language code: {{a, b, c}, {d, e, f}, {g, h, i}}[[2]]

This is followed by extracting the third column from that row:

Wolfram Language code: %[[3]]

The second column of a matrix:

Wolfram Language code: {{a, b, c}, {d, e, f}, {g, h, i}}[[All, 2]]

Pick out parts 1 and 3:

Wolfram Language code: {{a, b, c}, {d, e, f}, {g, h, i}}[[{1, 3}]]

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

Wolfram Language code: {{a, b, c}, {d, e, f}, {g, h, i}}[[{1, 3}, {2, 3}]]

Extract a subexpression from an association:

Wolfram Language code: assoc = <|"a" -> <|"c" -> "ac", 1 -> "a1"|>, "b" -> <|"c" -> "bc", 1 -> "b1"|>|>;
Wolfram Language code: assoc[["a", Key[1]]]

Extract several subexpressions:

Wolfram Language code: assoc[[All, Key[1]]]

Use a mixture of Key-based and numeric indices:

Wolfram Language code: assoc[["b", 1]]

Extract deep parts of arbitrary expressions:

Wolfram Language code: f[g[a, b], g[c, d]][[2, 1]]
Wolfram Language code: {x -> 4, y -> 5}[[1, 2]]

For SparseArray objects, Part gives the parts in the corresponding ordinary array:

Wolfram Language code: s = SparseArray[{{i_, j_} /; Abs[i - j] == 1 -> i - j}, {4, 4}]
Wolfram Language code: MatrixForm[s]
Wolfram Language code: s[[1, 2]]

Rows or columns are represented as sparse vectors:

Wolfram Language code: s[[All, 2]]

Values can also be set:

Wolfram Language code: s[[{1, 2}, {3, 4}]] = 1
Wolfram Language code: MatrixForm[s]

For structured arrays, Part gives the parts in the corresponding ordinary array:

Wolfram Language code: qa = QuantityArray[{{1.4, 2.3}, {2.8, 2.7}, {4.2, 3.5}}, {"Seconds", "Meters"}]

Parts from a QuantityArray are a Quantity object with units:

Wolfram Language code: qa[[1, 2]]

If the part is still an array, its head is preserved:

Wolfram Language code: qa[[1]]

Assignments  (6)

Reassign a nested part:

Wolfram Language code: m = {{a, b}, {c, d}}
Wolfram Language code: m[[2, 2]] = x
Wolfram Language code: m

Reassign a sequence of parts:

Wolfram Language code: v = {a, b, c, d, e, f}
Wolfram Language code: v[[2 ;; 4]] = x
Wolfram Language code: v

Assign several parts at once:

Wolfram Language code: m = {a, b, c, d}

Assign parts 2 and 3 to be x:

Wolfram Language code: m[[{2, 3}]] = x
Wolfram Language code: m

Assign different values to parts 1, 3, and 4:

Wolfram Language code: m[[{1, 3, 4}]] = {s, t, u}
Wolfram Language code: m

All standard assignment operations work on parts:

Wolfram Language code: m = {a, b, c, d};
Wolfram Language code: m[[2]] += x
Wolfram Language code: m

Rearrange elements by reassigning parts:

Wolfram Language code: m = {a, b, c, d};
Wolfram Language code: m[[{1, 2}]] = m[[{3, 1}]]
Wolfram Language code: m

For associations, it is possible to add keys using Part:

Wolfram Language code: assoc = <||>;
Wolfram Language code: assoc[["a"]] = 1
Wolfram Language code: assoc[[Key[3]]] = "five"
Wolfram Language code: assoc

Add a part to all the associations in a given level:

Wolfram Language code: listOfAssocs = {<||>, <|"key" -> "val"|>}; listOfAssocs[[All, "new"]] = 2; listOfAssocs

Input Forms  (4)

Enter in FullForm:

Wolfram Language code: Part[{a, b, c, d, e}, 3]

Enter a Span specification using FullForm:

Wolfram Language code: Part[{a, b, c, d, e}, Span[2, 4]]

Enter using [[ and ]]:

Wolfram Language code: {a, b, c, d, e}[[3]]

Enter as a subscript using :

Wolfram Language code: Subscript[{a, b, c, d, e}, [[3]]]

Use the special characters \[LeftDoubleBracket]and \[RightDoubleBracket]:

Wolfram Language code: Subscript[{a, b, c, d, e}, [[3]]]

Applications  (6)

Pick out the first solution from an equation:

Wolfram Language code: Solve[x ^ 2 + 5x + 1 == 0, x]
Wolfram Language code: %[[1, 1, 2]]

Pick out all solutions for a univariate equation:

Wolfram Language code: Solve[x ^ 2 + 5x + 1 == 0, x]
Wolfram Language code: %[[All, 1, 2]]

Go through the first 1000 primes, and count how many lie in each possible "mod 10 bin":

Wolfram Language code: m = Table[0, {10}];
Wolfram Language code: Do[m[[Mod[Prime[i], 10, 1]]]++, {i, 1000}]
Wolfram Language code: m

Another way to get the same result:

Wolfram Language code: Table[Count[Table[Mod[Prime[i], 10], {i, 1000}], j], {j, 0, 9}]

Apply a permutation:

Wolfram Language code: {a, b, c, d, e}[[{4, 5, 1, 2, 3}]]

Invert a permutation:

Wolfram Language code: pinv = ConstantArray[0, 5]; pinv[[{4, 5, 1, 2, 3}]] = Range[5]; pinv

Verify the result with a direct computation using InversePermutation:

Wolfram Language code: InversePermutation[{4, 5, 1, 2, 3}]

Pick out parts cyclically by using Mod with offset 1:

Wolfram Language code: Table[{a, b, c, d}[[Mod[i, 4, 1]]], {i, 15}]

Properties & Relations  (10)

The zeroth part of an expression is its head:

Wolfram Language code: (a + b + c)[[0]]
Wolfram Language code: {a, b, c}[[0]]
Wolfram Language code: {a + b, 2x}[[2, 0]]

Heads in the original expression are preserved when using a list of parts specification:

Wolfram Language code: f[a, b, c][[{2, 3}]]

This is true even when using a multilevel specification:

Wolfram Language code: f[g[a, b], h[c, d]][[{1, 2}, {2}]]

Part[expr,{}] yields Head[expr][]:

Wolfram Language code: Part[{1, 2, 3}, {}]
Wolfram Language code: f[1, 2, 3][[{}]]

For string keys, Key["key"] and "key" are equivalent:

Wolfram Language code: assoc = <|"a" -> f, 3 -> g, b -> h|>;
Wolfram Language code: {assoc[[Key["a"]]], assoc[["a"]]}

This is not true of general keys:

Wolfram Language code: {assoc[[Key[3]]], assoc[[3]]}
Wolfram Language code: {assoc[[Key[b]]], assoc[[b]]}

Part[expr] gives expr:

Wolfram Language code: Part[a]
Wolfram Language code: f[][[]]

Assigning to part 0 is effectively a form of Apply:

Wolfram Language code: t = {a, b, c}; t[[0]] = f; t

Perform the same operation using @@ (Apply):

Wolfram Language code: t = {a, b, c}; t = f@@t; t

Part[expr,m;;n] is effectively equivalent to Take[expr,{m,n}]:

Wolfram Language code: list = {a, b, c, d, e, f, g}
Wolfram Language code: list[[2 ;; 5]] === Take[{a, b, c, d, e, f, g}, {2, 5}]

Similarly, Part[expr,m;;n;;s] is effectively equivalent to Take[expr,{m,n,s}]:

Wolfram Language code: list[[1 ;; 5 ;; 2]] === Take[{a, b, c, d, e, f, g}, {1, 5, 2}]

Part[expr,i,j,…] is effectively equivalent to Extract[expr,{i,j,…}]:

Wolfram Language code: mat = {{a, b, c}, {d, e, f}, {g, h, i}};
Wolfram Language code: mat[[1, 2]] === Extract[mat, {1, 2}]

Part can extract rectangular blocks:

Wolfram Language code: mat = {{a, b, c}, {d, e, f}, {g, h, i}};
Wolfram Language code: mat[[{1, 2}, {2, 3}]]

Extract can return lists of parts at arbitrary positions:

Wolfram Language code: Extract[mat, {{1, 2}, {2, 3}}]

Permute and Part interpret permutations differently:

Wolfram Language code: {a, b, c, d, e}[[{4, 5, 1, 2, 3}]]
Wolfram Language code: Permute[{a, b, c, d, e}, {4, 5, 1, 2, 3}]

The two interpretations are related by InversePermutation:

Wolfram Language code: Permute[{a, b, c, d, e}, InversePermutation[{4, 5, 1, 2, 3}]]

Possible Issues  (5)

Position does not return part specifications in a form that can immediately be used by Part:

Wolfram Language code: Position[{1, 0, 0, 1, 0, 0, 1, 1}, 1]
Wolfram Language code: {1, 0, 0, 1, 0, 0, 1, 1}[[##]]&@@@%

Extract extracts parts specified in the way returned by Position:

Wolfram Language code: Extract[{1, 0, 0, 1, 0, 0, 1, 1}, {{1}, {4}, {7}, {8}}]

When using a list of parts, successive part extraction is not always equivalent to direct part extraction:

Wolfram Language code: {{a, b}, {c, d}}[[{2}, 2]]
Wolfram Language code: {{a, b}, {c, d}}[[{2}]][[2]]

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

Wolfram Language code: a = Hold[2x + 2 * 3]
Wolfram Language code: a[[1]]

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

Wolfram Language code: a[[1, 1]]
Wolfram Language code: a[[1]][[1]]

Only parts that already exist can be reassigned:

Wolfram Language code: m = {a, b, c, d}
Wolfram Language code: m[[5]] = x

Create a shared variable whose value is a large matrix:

Wolfram Language code: list = RandomReal[1, {10 ^ 4, 10 ^ 4}]; SetSharedVariable[list]

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

Wolfram Language code: AbsoluteTiming[ParallelEvaluate[Table[list[[i, $KernelID]], {i, 4}]]]

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

Wolfram Language code: AbsoluteTiming[ParallelEvaluate[Table[Part[Unevaluated[list], i, $KernelID], {i, 4}]]]

See Also

Span  First  Head  Last  Extract  Position  ReplacePart  MapAt  Take  PadLeft

Function Repository: Slice  LookupPart

Tech Notes

    ▪
  • Manipulating Elements of Lists
  • ▪
  • Picking Out Pieces of Algebraic Expressions
  • ▪
  • Getting Pieces of Lists
  • ▪
  • Parts of Expressions
  • ▪
  • Manipulating Lists by Their Indices
  • ▪
  • Operators

Related Guides

    ▪
  • Parts of Expressions
  • ▪
  • Elements of Lists
  • ▪
  • Parts of Matrices
  • ▪
  • List Manipulation
  • ▪
  • Operations on Vectors
  • ▪
  • Expression Structure
  • ▪
  • Permutations
  • ▪
  • Handling Arrays of Data
  • ▪
  • Computation with Structured Datasets
  • ▪
  • Associations
  • ▪
  • Formula Manipulation
  • ▪
  • Tabular Objects
  • ▪
  • Wolfram Language Syntax
  • ▪
  • Structural Operations on Expressions
  • ▪
  • GPU Computing
  • ▪
  • Binary Data
  • ▪
  • GPU Computing with NVIDIA
  • ▪
  • GPU Computing with Apple

Related Links

  • An Elementary Introduction to the Wolfram Language : Operations on Lists
  • An Elementary Introduction to the Wolfram Language : Parts of Lists

History

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)

Wolfram Research (1988), Part, Wolfram Language function, https://reference.wolfram.com/language/ref/Part.html (updated 2014).

Text

Wolfram Research (1988), Part, Wolfram Language function, https://reference.wolfram.com/language/ref/Part.html (updated 2014).

CMS

Wolfram Language. 1988. "Part." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2014. https://reference.wolfram.com/language/ref/Part.html.

APA

Wolfram Language. (1988). Part. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Part.html

BibTeX

@misc{reference.wolfram_2026_part, author="Wolfram Research", title="{Part}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/Part.html}", note=[Accessed: 12-November-2026]}

BibLaTeX

@online{reference.wolfram_2026_part, organization={Wolfram Research}, title={Part}, year={2014}, url={https://reference.wolfram.com/language/ref/Part.html}, note=[Accessed: 12-November-2026]}

Top
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.