Skip to main content

Section 11.3 Worked Example: Writing Lists - Shifting List Values

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.3.1

Problem: You were to write the Python code that will declare a list of integers with the values 5, 10, 15, … 55 (multiples of 5). But then you remember you were supposed to start at 1, not 5 - so write the Python code that will shift all the values to the right by 1. In other words, the value 5 will shift from index 0 to index 1. And then you can put the value 1 at index 0.

Subsection 11.3.2 SG1: Instantiating a list variable

We will use the generic name alpha for the list variable, because we do not know anything else about the list in this case.
The list should start with the multiples of five, skipping over the 0.
alpha = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55]

Subsection 11.3.3 SG4: Adding or changing value of a list element

To shift the values to the right, we just need to insert a new value at the beginning of the list. We can use the insert method to insert the value 1 at index 0.
alpha.insert(0, 1)

Subsection 11.3.4

Answer.
alpha = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55]
alpha.insert(0, 1)
To verify we have the correct values, we can print out the values of the list:
print(alpha)
Since we do not need to do anything with the values, we can just print the entire list out all at once.

Subsection 11.3.5 Practice Pages

You have attempted of activities on this page.