Extract
Details
- For integers i, j, …, Extract[expr,{i,j,…}] is equivalent to Part[expr,i,j,…]. »
- The position specifications used by Extract have the same form as those returned by Position and used in functions such as MapAt. »
- An individual position specification pos can also be of the more general form {part1,part2,…}, where the parti are Part specifications such as an integer i, All or Span[…]. »
- Specifications that extract multiple parts at one level retain the head at that level. »
- You can use Extract[expr,…,Hold] to extract parts without evaluation. »
- If expr is a SparseArray object or structured array, Extract[expr,…] extracts parts in the corresponding ordinary array. »
- Extract works on Association objects, using the same specification for keys as in Part. »
- Extract[pos][expr] is equivalent to Extract[expr,pos].
Examples
open all close allBasic Examples (5)
Extract the second part from an expression:
Extract[{1, 2, 3, 4}, {2}]Do it using the operator form:
Extract[{2}][{a, b, c, d}]Extract a nested part in an expression:
Extract[f[g[1, 2], h[3]], {1, 2}]Extract several nested parts from an expression:
Extract[f[g[1, 2], h[x ^ 2]], {{1, 2}, {2, 1, 1}}]Extract the second column of matrix:
Extract[(| | | |
| - | - | - |
| a | b | c |
| d | e | f |
| g | h | i |), {All, 2}]Extract the parts given by Position:
e = f[g[1, 2], {h[3]}];p = Position[e, _Integer]Extract[e, p]Scope (25)
Single versus Multiple Positions (5)
Extract a single element from a list:
Extract[{a, b, c, d, e}, {3}]Extract several elements from a list:
Extract[{a, b, c, d, e}, {{1}, {4}, {3}}]Extract the part at position {1,3} in a matrix:
mat = Array[a, {3, 3}];Extract[mat, {1, 3}]Extract the first and third parts (rows) of a matrix:
Extract[mat, {{1}, {3}}]Extract a length-one list of positions from the matrix:
Extract[mat, {{1, 3}}]Extract several elements of the matrix:
Extract[mat, {{1, 2}, {3, 3}, {2, 1}}]Extract the right-hand side of a rule and wrap the result in Hold to prevent evaluation:
Extract[x :> 1 + 1, {2}, Hold]Extract several parts from a list and wrap each part in Hold:
Extract[{a, b, c, d, e}, {{1}, {2}, {5}}, Hold]Forms Involving All (5)
Extract the second row of a matrix:
m = (| | | |
| - | - | - |
| 1 | 2 | 3 |
| 4 | 5 | 6 |
| 7 | 8 | 9 |);Extract[m, {2}]Extract[m, {2, All}]Extract the second column of a matrix:
Extract[m, {All, 2}]Extract the first and third columns as a
submatrix:
Extract[m, {All, {1, 3}}]%//MatrixFormExtract the first and third columns separately, effectively creating a
matrix:
Extract[m, {{All, 1}, {All, 3}}]%//MatrixFormExtract the first rule from each list in a list of lists of rules:
rules = {{a -> 1, b -> 2}, {c -> 3, d -> 4, e -> 5}, {f -> 6}};Extract[rules, {All, 1}]Extract the right-hand sides from all the rules in the second list:
Extract[rules, {2, All, 2}]Extract the left-hand sides from all the rules in the list of lists:
Extract[rules, {All, All, 1}]Extracting using All preserves the head at that level:
Extract[e[f[1, 2, 3], g[4, 5, 6], h[7, 8, 9]], {All, 1}]Extract all the right-hand sides of a list of rules, preventing evaluation using Hold:
Extract[{a :> (1/0), b :> ArcTan[0, 0], c :> Print[5]}, {All, 2}, Hold]Without the Hold, the expression would evaluate and produce side effects:
Extract[{a :> (1/0), b :> ArcTan[0, 0], c :> Print[5]}, {All, 2}]Separately extract the two sides in a list of rules, wrapping the two results in HoldComplete:
Extract[{a :> (1/0), b :> ArcTan[0, 0], c :> Print[5]}, {{All, 1}, {All, 2}}, HoldComplete]Associations (6)
Extract a part from an association by key:
Extract[<|expr -> 1, "str" -> 2|>, {Key[expr]}]If the key is a string, the Key wrapper is optional:
Extract[<|expr -> 1, "str" -> 2|>, {"str"}]Extract a part from an association by key:
Extract[<|2 -> 1 + x ^ 2, 3 -> y ^ 2 - 1, 1 -> x + y + z|>, {Key[1]}]Extract a part from an association by position:
Extract[<|2 -> 1 + x ^ 2, 3 -> y ^ 2 - 1, 1 -> x + y + z|>, {1}]Extract multiple parts from an association:
Extract[<|2 -> 1 + x ^ 2, 3 -> y ^ 2 - 1, 1 -> x + y + z|>, {{1}, {Key[1]}}]Extract a nested part from an association:
Extract[<|2 -> 1 + x ^ 2, 3 -> -1 + y ^ 2, 1 -> x + y + z|>, {Key[3], 1}]Extract multiple nested parts from an association:
Extract[<|2 -> 1 + x ^ 2, 3 -> -1 + y ^ 2, 1 -> x + y + z|>, {{Key[3], 1}, {3, 1}}]Extract several values from an association without evaluation:
Extract[<|"a" :> 1 / 0, "b" :> Log[0]|>, {{"a"}, {"b"}}, Hold]Without the Hold, the values in the association would evaluate and produce messages:
Extract[<|"a" :> 1 / 0, "b" :> Log[0]|>, {{"a"}, {"b"}}]Special Array Types (3)
If expr is a sparse array, Extract[expr,…] extracts parts in the corresponding ordinary array:
sparse = SparseArray[{{1, 3} -> 1, {2, 2} -> 2, {3, 1} -> 3}, {3, 3}]Extract[sparse, {2, 2}]Subarrays are returned as a SparseArray object:
Extract[sparse, {All, 2}]Extract the first and third columns as a single sparse array:
sparse = SparseArray[{{1, 3} -> 1, {2, 2} -> 2, {3, 1} -> 3}, {3, 3}]Extract[sparse, {All, {1, 3}}]%//NormalExtracting as two separate columns returns a list of sparse arrays:
Extract[sparse, {{All, 1}, {All, 3}}]Converting to an ordinary array shows the result is the transpose of the first extraction:
%//NormalExtracting from a structured array extracts parts in the corresponding ordinary array:
QuantityArray[{{1, 2}, {3, 4}, {5, 6}}, {"Meters", "Seconds"}]Extract[%, {1, 1}]When possible, the result is returned as another structured array:
SymmetrizedArray[{{1, 1} -> 3, {2, 2} -> 1, {3, 1} -> -5}, {3, 3}, Symmetric[All]]Extract[%, {1}]Spans and Sublists (6)
Extract the third through fifth elements of a list:
Extract[Range[10], {3 ;; 5}]Extract the elements at odd positions:
Extract[{a, b, c, d, e, f}, {1 ;; -1 ;; 2}]Extract a span from a general expression, which keeps the head:
Extract[f[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], {-1 ;; 1 ;; -3}]Extract the first two elements from each list:
Extract[{{1, 2}, {3, 4, 5}}, {All, 1 ;; 2}]Extract the first and last element from each list:
Extract[{{1, 2}, {3, 4, 5}}, {All, {1, -1}}]Extract the first element of the second through fourth rows:
m = Array[a, {5, 5}];Extract[m, {2 ;; 4, 1}]Extract the second element of the first, third and fourth rows:
Extract[m, {{1, 3, 4}, 2}]Extract the first, second and fifth elements of the last two rows:
Extract[m, {-2 ;; -1, {1, 2, 5}}]Extract the first and fifth columns:
Extract[m, {All, {1, 5}}]Extract the right-hand side of the first two rules in the list and prevent evaluation:
Extract[{a :> 1 ^ 2, b :> 2 ^ 2, c :> 3 ^ 3}, {1 ;; 2, 2}, Hold]Properties & Relations (6)
If pos is not a list, pos and {pos} are equivalent specifications:
Extract[Range[10], 3] == Extract[Range[10], {3}]Extract[Range[10], 3 ;; 5] == Extract[Range[10], {3 ;; 5}]For a list of integers {i,j,…}, Extract[expr,{i,j,…}] is equivalent to Part[expr,i,j,…]:
Extract[{ArcTan[f[x], g[y]]}, {1, 2, 1}] === Part[{ArcTan[f[x], g[y]]}, 1, 2, 1]Extract[expr,{{i1,i2,…},{j1,j2,…},…}] is equivalent to {Part[expr,i1,i2,…],Part[expr,j1,j2,…],…}:
m = RandomReal[1, {3, 3}];
Extract[m, {{1 ;; 2, 1}, {2 ;; 3, 2}}] === {Part[m, 1 ;; 2, 1], Part[m, 2 ;; 3, 2]}This extends to the empty list:
Extract[m, {}] === {}Extract is the inverse of Position:
expr = {{a, b, c}, {c, {a}, d}, {{e}, a, f}};Position[expr, a]Extract[expr, %]In Extract[expr,{pos1,pos2,…},h], h wraps the result of extracting each of the posi:
expr = Partition[Range[9], 3]Extract[expr, {{1, 1}, {All, 3}}, h]If only a single position spec is given, h will be the outermost head:
Extract[expr, {All, 1}, h]Specifications that extract multiple parts at one level retain the head at that level:
expr = f[{1, 2}, {3, 4, 5}, {6}, {7, 8, 9, 10}];Extract[expr, {All, 1}]Extract[expr, {1 ;; 3, 1}]Extract[expr, {{2, 4}, 1}]Possible Issues (2)
Extract[expr,{}] returns an empty list of subexpressions because the list of positions is empty:
Extract[h[a, b], {}]Extract[expr,{{}}] extracts the subexpression at position {}, which is the whole expression:
Extract[h[a, b], {{}}]A list of lists in the second argument is always interpreted as a list of position specifications:
arr = Array[a, {2, 4}];Extract[arr, {{1, 2}}]As a result, specifications that are equivalent in Part may be treated differently by Extract:
Part[arr, {1, 2}] === Part[arr, 1 ;; 2]Extract[arr, {{1, 2}}] === Extract[arr, {1 ;; 2}]Use a list of lists and remove the extra list at the end if consistency between Part and Extract is needed:
First@Extract[arr, {{{1, 2}}}] === First@Extract[arr, {{1 ;; 2}}]Tech Notes
Related Guides
History
Introduced in 1996 (3.0) | Updated in 2003 (5.0) ▪ 2012 (9.0) ▪ 2014 (10.0) ▪ 2021 (13.0)
Text
Wolfram Research (1996), Extract, Wolfram Language function, https://reference.wolfram.com/language/ref/Extract.html (updated 2021).
CMS
Wolfram Language. 1996. "Extract." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2021. https://reference.wolfram.com/language/ref/Extract.html.
APA
Wolfram Language. (1996). Extract. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Extract.html
BibTeX
@misc{reference.wolfram_2026_extract, author="Wolfram Research", title="{Extract}", year="2021", howpublished="\url{https://reference.wolfram.com/language/ref/Extract.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_extract, organization={Wolfram Research}, title={Extract}, year={2021}, url={https://reference.wolfram.com/language/ref/Extract.html}, note=[Accessed: 12-June-2026]}