next | previous | forward | backward | up | index | toc

# take -- Take some elements from a list or sequence.

## Synopsis

• Usage:
take(L, i)
take(L, {j,k})
take(x, i)
• Inputs:
• Outputs:
• L2, , the list or sequence containing the first i elements of L, (if i positive), or the last i elements of L (if i negative), or, if given the pair j,k, the list or sequence containing the elements of L with indices j through k

## Description

 i1 : take({a,b,c,d,e,f,g}, 3) o1 = {a, b, c} o1 : List i2 : take({a,b,c,d,e,f,g}, -3) o2 = {e, f, g} o2 : List i3 : take({a,b,c,d,e,f,g}, {1,3}) o3 = {b, c, d} o3 : List i4 : take({a,b,c,d,e,f,g}, {2,2}) o4 = {c} o4 : List

The pair $\{j,k\}$ must be given with both entries non-negative, and $j\le k$. Otherwise an empty list is returned.

 i5 : take({a,b,c,d,e,f,g}, {3,1}) o5 = {} o5 : List i6 : take({a,b,c,d,e,f,g}, {4,-1}) o6 = {} o6 : List

If x is any object belonging to a class with the iterator method installed, then a list containing the first $i$ (or the $(j+1)$th through $(k+1)$th) objects returned when next is called on the output of iterator x is returned. If fewer than $i$ (or $k+1$) objects are returned before the StopIteration symbol is encountered, then the list will have shorter than expected length.

 i7 : take("Hello, world!", 5) o7 = {H, e, l, l, o} o7 : List i8 : take("Hello, world!", 20) o8 = {H, e, l, l, o, ,, , w, o, r, l, d, !} o8 : List i9 : take("Hello, world!", {7, 11}) o9 = {w, o, r, l, d} o9 : List

• drop -- drop some elements from a list or sequence.
• select -- select from a list, hash table, or string
• position -- the first element of a list satisfying a condition
• positions -- which elements of a list satisfy a condition
• lists and sequences -- a detailed overview of lists and sequences in Macaulay2

## Ways to use take :

• take(BasicList,List)
• take(BasicList,ZZ)
• take(Thing,List)
• take(Thing,ZZ)

## For the programmer

The object take is .