Skip to main content

Section 11.1 Worked Example: Lists - Instantiate and Alter

Subgoals for Evaluating Lists.

  1. Declaring and initializing a list
    1. Set up a one dimensional table (i.e., one row) with 0 to size - 1 elements.
    2. Upon instantiation of a list, all elements contain values from the initializer list (i.e., the list of values inside the square brackets). If no initializer list is provided, the list is empty.
  2. Determining access, slicing, changing, adding, or whole list actions.
    1. Determine the list name and the action to be performed.
    2. If the list is on the left hand side of an assignment statement WITHOUT square brackets, it is a Whole List Action for List Assignment.
    3. If the list is on the left hand side of an assignment statement WITH square brackets, it is a Changing value of a List Element.
    4. If the append or insert method is being used on an instance of a list, it is an Adding a Value to a List Element.
    5. If the list is an expression WITHOUT square brackets, it is a Whole List Action for Passing a List as an Argument.
    6. If the list is an expression WITH square brackets, then check if there is a colon in the square brackets. If there is a colon inside of the square brackets, it is a Slicing Multiple Values from a List. Otherwise, it is an Accessing List Element.
  3. 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.
  4. 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
  5. Changing value of a list element
    1. Evaluate expression within [] brackets to determine the index of the element to be changed, and the list to change.
      Determine value of index for element to be changed; a positive value if counting from the beginning, or a negative value if counting from the end.
    2. Determine the expression of RHS (right-hand side) of the assignment statement.
    3. The lists’ value is now changed to match the value calculated from the RHS of the assignment statement.
  6. Adding a value to a list element
    1. Check whether the append or insert method is being used. Note that either way you do not use an assignment statement, it is just a method call.
    2. If append is used, the new value is added 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. Existing values starting from that index are shifted to the right.
  7. Traversing a List
    1. Determine the list that is being iterated over. If the expression also involves a range(len(list_name)), then the list is being traversed by index. The range function can either take one argument (the length of the list) or 3 arguments (the starting index, ending index, and step size). If the range function takes one argument, it will start at 0 and go to the length of the list - 1. If the range function takes 3 arguments, it will start at the first argument and go to the second argument - 1, incrementing by the third argument. Otherwise, if range is not used, then the list is being traversed by value.
    2. Determine the loop control variable that is being used to iterate over the list. The loop control variable will take on each value or index in the list, one at a time, depending on whether we are iterating by value or by index.
    3. The loop control variable is used to access the list element in the body of the loop. If iterating by index, the loop control variable is used as an index to access or update the list element. If iterating by value, the loop control variable is used directly to access the list element (no updates are possible).
    4. The list can also be added to with the append or insert methods. The append method adds a new value to the end of the list, while the insert method adds a new value at the specified index. Existing values starting from that index are shifted to the right.
  8. 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. The list itself is not copied, so the function does not have its own copy of the list. However, the one exception to this is if you assign the argument to reference a different list in memory; then you will no longer be modifying the original list.
    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:
Evaluate these statements and determine the value of all variables. If any error occurs, give the reason.
alpha = [0, 0, 0, 0, 0]
alpha[4] = 22
alpha[0] = 10
alpha[1] = alpha[4] - alpha[0]
alpha[2] = alpha[1] - alpha[0]
alpha[3] = alpha[alpha[2] - 1]
alpha[4] = alpha[alpha[3]]

Subsection 11.1.2 SG1: Declaring and initializing a list

alpha = [0, 0, 0, 0, 0]
The figure shows a table with 6 columns and 2 rows. The top row is labeled with the indexes of the list, and the bottom row is labeled with the values of the list. The first column is labeled 0, and the last column is labeled 4. The second row contains all zeros.
Figure 11.1.1.
  • alpha is declared as a list of integers.
  • This statement allocates 5 slots for integers (first line are indexes, second line are values/content):
  • The declared size of the list is 5, so the last valid index value is 4.
  • Notice that the list contains values of 0 because there was a 0 in each of the slots in the list.

Subsection 11.1.3 SG2: Determine access or action

alpha[4] = 22
alpha[0] = 10
alpha[1] = alpha[4] - alpha[0]
alpha[2] = alpha[1] - alpha[0]
alpha[3] = alpha[alpha[2] - 1]
alpha[4] = alpha[alpha[3]]
In this example, whenever alpha[index] is used on the LHS of an assignment statement, then we will be changing the list element. When alpha[index] is used on the RHS of an assignment statement, then we are accessing an element.
For each of the lines in the example code, we will walk through the evaluation steps indicating the appropriate subgoals.

Subsection 11.1.4

alpha[4] = 22
alpha[0] = 10
For the first statement, we will be changing the value of a list element (SG5).
First, we evaluate the expression within [ ], which is just the literal value 4, which is within the bounds of the list (index 0 to index 4). We will be changing the value at index 4 within the list. So the last element (or element at index 4) will become the value 22.
For the second statement, we will be changing the first value (or element at index 0) to 10, which is also an integer and index 0 is within the list bounds.
The list alpha now contains the values:
The same table as before, but now the first and last elements are 10 and 22 respectively.
Figure 11.1.2.

Subsection 11.1.5

alpha[1] = alpha[4] - alpha[0]
For this statement we are changing the value of an list element (SG5) and also accessing list elements (SG3).
For SG5, we will be changing the element at index 1 within the list. We now have to determine the value to be placed at that index, which is the value of the expression on the RHS of the assignment statement. This means we will need to access list elements - so let’s look at SG3.
In looking at the RHS: alpha[4] - alpha[0]
Evaluating what is within the [ ] is easy for both accesses, as they are literal values and both values are within list bounds. So we replace the list access with its value from the list:
alpha[4] - alpha[0]
22 - 10
12
So the value 12 (an integer) is copied into index 1 of the list.
The same table as before, but now the second element is 12.
Figure 11.1.3.

Subsection 11.1.6

alpha[2] = alpha[1] - alpha[0]
For this statement we are changing the value of an list element (SG5) and also accessing list elements (SG3).
For SG5, we will be changing the element at index 2 within the list. We now have to determine the value to be placed at that index, which is the value of the expression on the RHS of the assignment statement. This means we will need to access list elements - so let’s look at SG3.
In looking at the RHS: alpha[1] - alpha[0]
Evaluating what is within the [ ] is easy for both accesses, as they are literal values and both values are within list bounds. So we replace the list access with its value from the list:
alpha[1] - alpha[0]
12 - 10
2
So the value 2 (an integer) is copied into index 2 of the list.
The same table as before, but now the third element is 2.
Figure 11.1.4.

Subsection 11.1.7

alpha[3] = alpha[alpha[2] - 1]
For this statement we are changing the value of an list element (SG5) and also accessing list elements (SG3).
For SG5, we will be changing the element at index 3 within the list (within list bounds). We now have to determine the value to be placed at that index, which is the value of the expression on the RHS of the assignment statement. This means we will need to access list elements - so let’s look at SG3.
In looking at the RHS: alpha[alpha[2]-1]
Evaluating what is within the [ ] requires another list access. Notice that we are basically reading this from the inside to the outside. So the first access is alpha[2] (2 is within the bounds of the list) and the value is 12.
alpha[alpha[2] - 1]
alpha[2 - 1]
alpha[1]
12
So the value 12 (an integer) is copied into index 3 of the list.
The same table as before, but now the fourth element is 12.
Figure 11.1.5.

Subsection 11.1.8

alpha[4] = alpha[alpha[3]]
For this statement we are changing the value of an list element (SG5) and also accessing list elements (SG3).
For SG5, we will be changing the element at index 4 within the list (within list bounds). We now have to determine the value to be placed at that index, which is the value of the expression on the RHS of the assignment statement. This means we will need to access list elements - so let’s look at SG3.
In looking at the RHS: alpha[alpha[3]]
evaluating what is within the [ ] requires another list access.
alpha[alpha[3]]
alpha[12]
12 is not a valid index, it is out of bounds of the list, so an IndexError exception occurs.

Subsection 11.1.9 Practice Pages

You have attempted 1 of 2 activities on this page.