# Finding a subsequence or a sublist

### 5.39.7 Finding a subsequence or a sublist

The bracket notation used to find elements of sequences and lists can also be used to extract a range of elements. If S is a sequence or list of size n, then S[n1..n2] returns the subsequence or sublist of S consisting of the elements with indices from n1 to n2, where 0 ≤ n1≤ n2 < s (in Xcas syntax mode) or 0 < n1≤ n2 ≤ s in other syntax modes.

#### Examples.

- Input:
  [0,1,2,3,4][1..3]
  Output:

  ⎡ ⎣ | 1,2,3 | ⎤ ⎦

- Input:
  (A,B,C,D,E)[1..3]
  Output:

  B,C,D

For lists, the at command can also be used to get a sublist.

- at takes two arguments:
  - L, a list.
  - n1..n2, a range of integers.
- at(L,n1..n2) returns the sublist of L consisting of the elements with indices from n1 to n2.

Again, at can not be used for sequences.

#### Example.

Input:

at([1,2,3,4,5],2..4

Output:

⎡ ⎣ | 3,4,5 | ⎤ ⎦

An alternative to using at for finding a sublist is the mid command, which again cannot be used for sequences.

- mid takes two mandatory and one optional argument:
  - L, a list.
  - n, the index of the beginning of the sublist.
  - Optionally, l, the length of the sublist.
- mid(L,n ⟨,l⟩) returns the sublist of L with index beginning at n. With the option l, the length of the sublist will be l, otherwise it will go to the end of the list L.

#### Examples.

- Input:
  mid([0,1,2,3,4,5],2,3)
  Output:

  ⎡ ⎣ | 2,3,4 | ⎤ ⎦

- Input:
  mid([0,1,2,3,4,5],2)
  Output:

  ⎡ ⎣ | 2,3,4,5 | ⎤ ⎦