List Construction

We start with a description of the basic list building commands. The simplest are
Range and Table. The first, Range, generates a list of numbers in arithmetic
progression. The format is

Range[ min, max,step]

In[1]:=

  Range[ 1, 90, 8]

Out[1]=

  {1, 9, 17, 25, 33, 41, 49, 57, 65, 73, 81, 89}

In[2]:=

  Range[ 0.1, 3.0, 0.6]

Out[2]=

  {0.1, 0.7, 1.3, 1.9, 2.5}

The Table command is more general as it can take a function as its first argument.

In[3]:=

  Table[ i^2, { i, 1, 5}]

Out[3]=

  {1, 4, 9, 16, 25}

Table can take more than one iterator.

In[4]:=

  Table[ x^2+y^2, {x,1,3}, {y,1,4}]

Out[4]=

  {{2, 5, 10, 17}, {5, 8, 13, 20}, 
    {10, 13, 18, 25}}

The result is a list of lists in which the first list has x fixed to 1 and y varies from 1 to
4, then x is fixed to 2 and so on. The additional braces can be removed by using
the Flatten command.

We can add elements to an existing list by using the Append, Prepend and Insert
commands. To combine two lists, use the Union and Join commands.

In[5]:=

  list= {};
  list= Append[ list, 3]
  list= Append[ list, 5]
  

  General::spell1: 
Possible spelling error: new symbol name
"list" is similar to existing symbol "List".




;[o]
General::spell1:
Possible spelling error: new symbol name
"list" is similar to existing symbol "List".

Out[5]=

  {3}

Out[6]=

  {3, 5}

We see that Append adds the element to the end of the list. Append returns
a list with the element added, but does not modify the argument. For example,
list1= Append[ list, 7] will not modify list.

In[7]:=

  list1= Append[ list, 7];
   list
  list1

Out[7]=

  {3, 5}

Out[8]=

  {3, 5, 7}

To modify the argument so that an additional assignment is not necessary, use
the AppendTo command.
AppendTo[ list, element] will add the element to the list.

The corresponding functions for inserting elements at the beginning of a list are
Prepend and PrependTo.

To insert an element into a list at an arbitrary position, use Insert. The structure
is Insert[list,element, position]. If the specified position is a negative integer, then the element
is inserted from the end of the list.

In[9]:=

  list= { 3, 5, 7, 13};
  list1= Insert[ list, 11, -2]

Out[9]=

  {3, 5, 7, 11, 13}

To combine lists, use the Union and Join commands. Union removes any duplicates
and sorts the result while Join appends the second list to the end of the first..

In[10]:=

  list1= { 1,1,2,3,5,8,13,21};
  list2= { 3, 5, 7,11,13};
  Union[ list1, list2]
  Join[list1,list2]

Out[10]=

  {1, 2, 3, 5, 7, 8, 11, 13, 21}

Out[11]=

  {1, 1, 2, 3, 5, 8, 13, 21, 3, 5, 7, 11, 13}

Up to Lists