Extract

Extract[expr,pos]

extracts the part of expr at the position specified by pos.

Extract[expr,{pos1,pos2,}]

extracts a list of parts of expr.

Extract[expr,pos,h]

extracts parts of expr, wrapping each of them with head h before evaluation.

Extract[pos]

represents an operator form of Extract that can be applied to an expression.

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 and ReplacePart. »
  • 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 allclose all

Basic Examples  (5)

Extract the second part from an expression:

Do it using the operator form:

Extract a nested part in an expression:

Extract several nested parts from an expression:

Extract the second column of matrix:

Extract the parts given by Position:

Scope  (25)

Single versus Multiple Positions  (5)

Extract a single element from a list:

Extract several elements from a list:

Extract the part at position {1,3} in a matrix:

Extract the first and third parts (rows) of a matrix:

Extract a length-one list of positions from the matrix:

Extract several elements of the matrix:

Extract the right-hand side of a rule and wrap the result in Hold to prevent evaluation:

Extract several parts from a list and wrap each part in Hold:

Forms Involving All  (5)

Extract the second row of a matrix:

Equivalently:

Extract the second column of a matrix:

Extract the first and third columns as a submatrix:

Extract the first and third columns separately, effectively creating a matrix:

Extract the first rule from each list in a list of lists of rules:

Extract the right-hand sides from all the rules in the second list:

Extract the left-hand sides from all the rules in the list of lists:

Extracting using All preserves the head at that level:

Extract all the right-hand sides of a list of rules, preventing evaluation using Hold:

Without the Hold, the expression would evaluate and produce side effects:

Separately extract the two sides in a list of rules, wrapping the two results in HoldComplete:

Associations  (6)

Extract a part from an association by key:

If the key is a string, the Key wrapper is optional:

Extract a part from an association by key:

Extract a part from an association by position:

Extract multiple parts from an association:

Extract a nested part from an association:

Extract multiple nested parts from an association:

Extract several values from an association without evaluation:

Without the Hold, the values in the association would evaluate and produce messages:

Special Array Types  (3)

If expr is a sparse array, Extract[expr,] extracts parts in the corresponding ordinary array:

Subarrays are returned as a SparseArray object:

Extract the first and third columns as a single sparse array:

Convert to an ordinary array:

Extracting as two separate columns returns a list of sparse arrays:

Converting to an ordinary array shows the result is the transpose of the first extraction:

Extracting from a structured array extracts parts in the corresponding ordinary array:

When possible, the result is returned as another structured array:

Spans and Sublists  (6)

Extract the third through fifth elements of a list:

Extract the elements at odd positions:

Extract a span from a general expression, which keeps the head:

Extract the first two elements from each list:

Extract the first and last element from each list:

Extract the first element of the second through fourth rows:

Extract the second element of the first, third and fourth rows:

Extract the first, second and fifth elements of the last two rows:

Extract the first and fifth columns:

Extract the right-hand side of the first two rules in the list and prevent evaluation:

Properties & Relations  (6)

If pos is not a list, pos and {pos} are equivalent specifications:

For a list of integers {i,j,}, Extract[expr,{i,j,}] is equivalent to Part[expr,i,j,]:

Extract[expr,{{i1,i2,},{j1,j2,},}] is equivalent to {Part[expr,i1,i2,],Part[expr,j1,j2,],}:

This extends to the empty list:

Extract is the inverse of Position:

In Extract[expr,{pos1,pos2,},h], h wraps the result of extracting each of the posi:

If only a single position spec is given, h will be the outermost head:

Specifications that extract multiple parts at one level retain the head at that level:

Possible Issues  (2)

Extract[expr,{}] returns an empty list of subexpressions because the list of positions is empty:

Extract[expr,{{}}] extracts the subexpression at position {}, which is the whole expression:

A list of lists in the second argument is always interpreted as a list of position specifications:

As a result, specifications that are equivalent in Part may be treated differently by Extract:

Use a list of lists and remove the extra list at the end if consistency between Part and Extract is needed:

Wolfram Research (1996), Extract, Wolfram Language function, https://reference.wolfram.com/language/ref/Extract.html (updated 2021).

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_2024_extract, author="Wolfram Research", title="{Extract}", year="2021", howpublished="\url{https://reference.wolfram.com/language/ref/Extract.html}", note=[Accessed: 17-November-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_extract, organization={Wolfram Research}, title={Extract}, year={2021}, url={https://reference.wolfram.com/language/ref/Extract.html}, note=[Accessed: 17-November-2024 ]}