More with Lists: Cloning and Nesting

 


Introduction

The purpose of this lab is to gain additional practice working with lists. It explores how to copy (clone) a list and introduces multi-dimensional lists.



Copying (Cloning)

We often want to modify a list and also keep a copy of the original list. In this situation, we need to be able to make a copy of the lsit itslef, not just the reference to the list. This process is sometimes called cloning, to avoid ambiguity of the word copy. In the following exercises we will explore several methods to make a clone of a list.
  1. In Google Colab, create a new notebook for the functions you will write in this activity. Then save the new notebook with a name representative of this activity.
  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. Add a text cell and write down what you saw happening.

  3. In the next couple of exercises, you will experiment with three different methods to actually make a clone of a list.

  4. Write a function, clone1, that takes a list as a parameter and returns a new list that is a clone 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.
  5. Write a function, clone2, that takes a list as a parameter and returns a new list that is a clone 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.
  6. Write a function, clone3, that takes a list as a parameter and returns a new list that is a clone 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.
  7. In a new Text cell, explain which of the 3 methods to create a clone is the easiest and why.

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, 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 with initial value 0, which will be used to keep track of the total. This 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 values.
  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 values. (You should also calculate the averages on a calculator so that you know you are getting the correct result.)

Multi-dimensions

In this section, we will learn how to create two-dimensional lists and how to access elements within these structures.
  1. Copy the following program that creates a two-dimensional list of random numbers.
        import random
    
        # Create 2-dimensional list
        values = [[0,0,0,0],[0,0,0,0],[0,0,0,0]]
        
        # Fill list with random values
        for r in range(len(values)):
            for c in range(len(values[0])):
                values[r][c] = random.randint(1,100)
        
        # Display the random numbers
        print(values)
        
  2. Run this program several times. In a new Text cell, explain what you see happening.

  3. You should notice that all of the elements of the list are also lists, but they get printed all on one line. Instead of printing the entire list at once, write code statements that will print one row (i.e., one element of the list) at a time. You will need a loop to do this.
  4. Modify the printing so that it prints one number from the two-dimensional list (in the previous exercise) on each line.
  5. Modify your list so that 0 is assigned to the elements in the second column.
  6. Write a function that takes a two-dimensional list of random integers as its parameter and replaces all of the even integers with a 1.

  7. Create several two-dimensional lists of random numbers and test your function.

  8. Write a function that takes a two-dimensional list as its parameter, and returns the sum of all elements in that list. You will need a variable to keep track of the sum, and will need nested loops to access all of the elements.
  9. Test your function with several two-dimensional lists.

  10. The list we used in this set of exercises has been of size 3x4. It wasn't too tedious to create the list by typing in all the 0's for a list of that size. Now suppose we wanted a list of size 200x300. No one wants to type in that many zeros! To create a list of size 200x300, we could use the following:
        myList = [[0 for j in range(300)] for i in range(200)]
        
    This will create a list with 200 elements, where each of those elements is a list with 300 0s. Type this in and print the list.

  11. Modify the code above that creates the 2-dimensional list of random numbers so that the list size is determined by the user. Ask the user for the list dimensions (# of elements/lists, # of elements per list). Then use those user-specified values to create the list.

  12. Run your code to test that you have the correct size list. The code from exercises 2 - 9 should still work correctly with this new list. Test those functions and make any necessary modifications.

Submit