Lab: More with Lists

 


Introduction

The purpose of this lab is to gain additional practice working with lists. It looks at how to copy lists, process lists, use the special Python syntaxes of list comprehension, the zip function, and the sum iterator.



List Copying

  1. In the Editor in Spyder, create a new file for the functions you will write in this lab. Then save the new file with a name representative of this lab.
  2. (Tempting way to copy lists that DOESN'T work)
    Consider the following code:
              list = [1, 2, 3]
              newList = list
              print(list, newList)
              
    It looks like this code would put the elements of list into the newList. Add a line of code after the print statement that changes the second element of newList to 5. Then print both lists again. What happens? In the next couple of exercises, you will experiment with three different methods to actually make a copy of a list.
  3. Write a function, copy1 that takes a list as a parameter and returns a new list that is a copy of the original list. To create this copy, your function should first create an empty list. It should then loop through the original list, and append each element to the new list. When the loop is finished,the function returns the new list.
  4. Write a function, copy2 that takes a list as a parameter and returns a new list that is a copy of the original list. To create this copy, your function should create an empty list and use the concatenate operator (+) with the empty new list and the original list. The function should then return the new list.
  5. Write a function, copy3 that takes a list as a parameter and returns a new list that is a copy of the original list. To create this copy, your function should use slicing on the original list to get all of the elements. Your function should then return the new list.

Processing Lists

To work with the elements of a list, it is quite common to use a for loop to iterate over the elements of the list, using one of these patterns:
                               
    for item in list_name:                  for index in range(len(list_name)):
        do something with item                  do something with list_name[index]
    
We will use these patterns in the next several exercises. (Try both patterns for one of the functions below, to see the difference.)

  1. Write a function named total that takes a list of numeric values (float or int) as a parameter and returns the cumulative total of all elements in the list. The function will need to create a variable (call it count or something similar) with initial value 0, which will be used to keep track of the total. Use one of the loop patterns above to iterate over all of the elements in the list. This count variable will get updated in the body of the loop that iterates over the elements of the list. At the end of the function, this variable gets returned.
  2. Test your function by passing in lists of various sizes and types.
  3. Write a function, average, that takes a list of numeric values as input and returns the average of the elements in the list. This function should look almost exactly like the total function, but will have one additional calculation.
  4. Test your function by passing in various lists of different sizes and types. (You should also calculate the averages on a calculator so that you know you are getting the correct result.)

List Comprehension

Python has a special, compact syntax, called list comprehension that allows us to run through one list and for each element create a new element in another list. (List comprehensions can be recognized as a for loop inside square brackets.) We will experiment with this in the next few exercises.
  1. Create a new list with 10 random Fahrenheit temperatures. You may use the random.randint function that we used in the earlier mini-lab on lists, or you may just create these temperatures and add them to the list yourself.

  2. Now we will use list comprehension to create a new list such that the new elements are 10 degrees more than the original temperatures. We can do this by writing a line of code that looks like:
    newTemps = [f+10 for f in temps]
    where temps is the original list of temperatures. This line of code is the same as:
    newTemps = []
    for f in temps:
       newTemps.append(f+10)
    Add this 1 new line of code to your file and test that it works.
  3. Create a new list of celsius temperatures that correspond to your original fahrenheit temperatures by using list comprehension.
  4. Print out the list of fahrenheit temps together with the corresponding celsius temps.
  5. In the previous exercise, you most likely used an indexed for loop to access the corresponding elements from both lists to print at one time. Python offers a special syntax to do this with the zip function. It turns a set of n lists into one list of n-tuples, where the element of the first n-tuple contains the first elements of each of the lists, and so on. So we could write something like:
    for f, c in zip(temps, newTemps):
        print f, c
    Add this code to your program and test that it works.
  6. Python has a built-in sum iterator that actually allows us to write the code for the total function that we wrote earlier in one line by using list comprehension. If we had a list called numbers, to get the sum of all of these numbers, we could write:
    count = sum(x for x in numbers)
    Create a second version of your total function from above called total2 that uses the sum iterator to calculate the total. The total should be returned at the end of the function.
  7. Use the same tests that you used on the first version of this function to make sure that this new version is working correctly.
  8. (OPTIONAL CHALLENGE, just for fun, not for credit) Write a small program (possibly 1 or more functions) that will print a nicely formatted table of t and y(t) values, where y(t) = v0t - (1/2)gt2. It should use n + 1 uniformaly spaced t values throughout the interval [0, 2v0/g]. The values for t should be calculated and stored in a list. List comprehension should be used to create the list of y values, and the zip function should be used to traverse the two lists for printing. (See Exercises 2.7, 2.8, and 2.9 in your textbook.)
    Stop and Think: What does this equation compute? What are the values of v0 and g in this formuala? Should either (or both) of them be parameters to your function(s)? (If you are unsure of how to answer any of these functions, please ask an instructor or TA.

Submit