Introduction to Computer Science
Union College
Spring 2009

Some list exercises

  1. Create a variable l1 which is pointing to a list with the five elements 1,2,3,4,5.

For the following, try to write solutions which are as general as possible. That is, try to find solutions that would also work if you didn't know how long the list was or what the elements of the list were.

  1. Write an expression that retrieves the 3rd element from the list (i.e.3).
  2. Write an expression that retrieves the sublist containing the third element up to the last element (i.e. [3,4,5]).
  3. Write an expression that retrieves the last element. Try to write it such that it would also work if you didn't know how long the list was.
  4. Write a statement that makes the first element in the list 100.
  5. Write a statement that adds the elements 200, 300, 400 to the end of the list.
  6. Write a statement that inserts the elements 1000, 2000 between the 2nd and 3rd element.
by Kristina Striegnitz