Getting Pieces of Lists
First[
list]
the first element in list
Last[
list]
the last element
Part[
list,
n]
or list[[
n]]
the n^(th) element
Part[
list,-
n]
or list[[-
n]]
the n^(th) element from the end
Part[
list,
m;;
n]
elements m through n
Part[
list,{
n1,
n2,
...}]
or list[[{
n1,
n2,
...}]]
the list of elements at positions , , ...
Picking out elements of lists.
We will use this list for the examples.
Here is the last element of .
This gives the third element.
This gives the list of elements through .
This gives a list of the first and fourth elements.
Take[
list,
n]
the first n elements in list
Take[
list,-
n]
the last n elements
Take[
list,{
m,
n}]
elements m through n (inclusive)
Rest[
list]
list with its first element dropped
Drop[
list,
n]
list with its first n elements dropped
Most[
list]
list with its last element dropped
Drop[
list,-
n]
list with its last n elements dropped
Drop[
list,{
m,
n}]
list with elements m through n dropped
Picking out sequences in lists.
This gives the first three elements of the list defined above.
This gives the last three elements.
This gives elements through inclusive.
This gives elements through in steps of .
This gives with the first element dropped.
This gives with its first three elements dropped.
This gives with only its third element dropped.
The functions here allow you to pick out pieces that occur at particular positions in lists.
"Finding Expressions That Match a Pattern" shows how you can use functions like
Select and
Cases to pick out elements of lists based not on their positions, but instead on their properties.