Skip to main content

Section 11.1 Worked Example: Writing Lists - Storing Multiplication Table

Subgoals for Writing Lists.

  1. Instantiating a list variable
    1. Craft name of variable
    2. Determine if the list is empty or contains initial values. The initial values can be specific values, but they can also be the result of calling a function like range to generate a sequence of numbers and then converting that to a list.
    3. Assign the values to be stored in the list using square brackets or the appropriate function call.
  2. Accessing list element
    1. Determine value of index for element to be accessed; a positive value if counting from the beginning, or a negative value if counting from the end.
    2. listName[index] returns value stored at that index
    3. Index must be between 0 and len(listName)-1, inclusive, or a negative value; otherwise an IndexError exception occurs at runtime
  3. Slicing multiple values from a list
    1. Determine the range of indexes for the elements to be sliced
    2. listName[startIndex:endIndex] returns a new list containing the elements from startIndex to endIndex-1 (inclusive)
    3. Negative numbers can be used for startIndex and endIndex to count from the end of the list
    4. Omitting startIndex starts from the beginning of the list, and omitting endIndex goes to the end of the list
  4. Adding or changing value of a list element
    1. Decide if adding a new value to the end, adding a value elsewhere in the list, or changing an existing value inside of the list.
    2. If adding to the end, use the append method to add the new value to the end of the list. Note that you do not use an assignment statement, it is just a method call.
      1. collection_name.append(new_value) - add a new element to the end of the list.
    3. If adding a value elsewhere in the list, use the insert method to add the new value at the specified index. Note that you do not use an assignment statement, it is just a method call.
      1. collection_name.insert(index, new_value) - add a new element at the specified index in the list.
    4. If changing a value already in the list, use an assignment statement to update the value at the appropriate index.
      1. Determine value of index of element to be changed (remember rules for index values)
      2. Determine the expression for RHS
      3. Write assignment statement to update list element
  5. Traversing a list
    1. Decide if updating in place or if only accessing.
    2. If accessing, write a normal for loop:
      1. for var_name in collection_name - traverses collection_name from first element to last element storing a copy of each element from collection_name in var_name for each iteration of the loop.
    3. If updating in place, write a for loop using range and len:
      1. for i in range(len(collection_name)) - traverses collection_name from first element to last element storing the index of each element in i for each iteration of the loop.
    4. Inside iteration, use loop control variable i as index into list, or var_name as value of list element
  6. Whole list actions
    1. Passing a list as an argument
      1. Determine that the entire list must be passed as an argument to a method by consulting documentation.
      2. When calling a function, put variable name that represents the list as an argument in the method call. (Remember that when passing a list as an argument that changes made by the function to the list are persistent.)
    2. List Assignment
      1. Determine that the reference to the list needs to be changed, not just its contents.
      2. The LHS of the assignment is the list reference needing to be changed
      3. The RHS of the assignment is the new list reference

Subsection 11.1.1

Problem: Write the Python code to store the multiplication table for the value 13. The first element should be 0 (13 * 0), the second element should be 13 (13 * 1), the third element should be 26 (13 * 2), etc. There should be a total of 51 elements in the list, representing the numbers 0 through 50.

Subsection 11.1.2 SG1: Instantiating a list variable

We will name the variable mult13. The list will be instantiated with a sequence of 51 numbers from 0 to 50. We will use the range function to generate the numbers from 0 to 50, incrementing by 1 each time, and then convert the range to a list.
mult13 = list(range(51))

Subsection 11.1.3 SG5: Traversing a list

Before we actually access or update the list (SG3 and SG4), it is important for us to realize that we will need to update all the elements of the list. As currently instantiated the list mult13 has the values 0, 1, 2, ..., 50. So we will first discuss the traversal of the list.
We will be traversing the entire list, updating all the elements. Because we will be updating the values within the list, we need to use the range function to generate the indices based on the length of the list.
for i in range(len(mult13)):

Subsection 11.1.4 SG4: Changing the value of a list element

Inside the loop, we want to change the current value stored in that list element (at index i). The expression for the RHS, should be the literal value 13 multiplied by the loop control variable (i). So the statement inside the loop should be:
mult13[i] = 13 * i

Subsection 11.1.5

Answer.
mult13 = list(range(51))
for i in range(len(mult13)):
    mult13[i] = 13 * i
To verify we have the correct values, we can print out the values of the list:
for i in range(len(mult13)):
    print("13 *", i, "is", mult13[i])
Although we are only accessing the values in the list, we still need to use the range function to generate the indices based on the length of the list. This is because we want to print the number that 13 is multiplied by (which is identical to the index). If we didn’t want to print out the number, then we could simply use a for loop that traverses the list directly.

Subsection 11.1.6 Practice Pages

You have attempted of activities on this page.