Take

Take[list,n]

gives the first n elements of list.

Take[list,-n]

gives the last n elements of list.

Take[list,{m,n}]

gives elements m through n of list.

Take[list,seq1,seq2,]

gives a nested list in which elements specified by seqi are taken at level i in list.

Details

  • Take uses the standard sequence specification:
  • Allall elements
    Noneno elements
    nelements 1 through n
    UpTo[n]elements 1 up to at most n, as available
    -nlast n elements
    {n}element n only
    {m,n}elements m through n inclusive
    {m,n,s}elements m through n in steps of s
  • Take can be used on an object with any head, not necessarily List.
  • Take[list,seq1,seq2] effectively extracts a submatrix from list.
  • Applying Take to a SparseArray object normally yields another SparseArray object.
  • Applying Take to an Association object yields an Association containing elements from the specified positions.

Examples

open allclose all

Basic Examples  (6)

Take the first 4 elements from a list:

Take the last 3 elements:

Take elements 2 through 4:

Take the second element from an Association:

Take all but the first element from an Association:

Take every other element from 2 to 6 from an Association:

Scope  (8)

Take elements from the second to the second-to-last:

Take every other element:

Take the first two rows of a matrix:

Take all rows, and the first two columns:

Take the first two rows, and the last column:

Take a submatrix:

Take every other row and column:

Take the first 4 elements if possible, else as many as are available:

Generalizations & Extensions  (2)

Take works with any head, not just List:

Take works with SparseArray objects:

Applications  (2)

Pick out a subimage:

Sample every third cell in an image:

Properties & Relations  (5)

Take and Drop are complementary:

Take is often equivalent to Part extraction with Range:

Take is equivalent to Part extraction with Span:

Take gives an array whose first dimensions are its arguments:

Reverse[e] is equivalent to Take[e,{-1,1,-1}]:

Possible Issues  (1)

Take always returns a list, even if it is just taking a single element:

Introduced in 1988
 (1.0)
 |
Updated in 1999
 (4.0)
2000
 (4.1)
2002
 (4.2)
2003
 (5.0)
2014
 (10.0)
2015
 (10.3)